2015-04-02 111 views
1

試圖爲Unity編寫一個腳本,該腳本需要使用C#將遊戲對象A的位置和旋轉分配給遊戲對象B.在Unity中旋轉游戲對象

調試日誌顯示我想要得到的正確的旋轉角度,但我不知道如何實際將該值分配給其他遊戲對象。

從今天開始,我就是C#的新手,所以它很可能是我的語法,但我對Unity也是一個相當新的東西。

在此先感謝!

using UnityEngine; 
using System.Collections; 

public class MoveArrow : MonoBehaviour { 

    void Start() { 
    } 

    void Update() { 
     var playerMapPos = GameObject.FindWithTag ("Player"); 
     var playerWorldPos = GameObject.FindWithTag ("PlayerCube"); 

     Debug.Log ("x: " + playerMapPos.transform.eulerAngles.x); 
     Debug.Log ("y: " + playerMapPos.transform.eulerAngles.y); 
     Debug.Log ("z: " + playerMapPos.transform.eulerAngles.z); 

     playerWorldPos.transform.rotation = Vector3(
      playerMapPos.transform.eulerAngles.x, 
      playerMapPos.transform.eulerAngles.y, 
      playerMapPos.transform.eulerAngles.z 
     ); 
    } 
} 

我得到以下錯誤:

Assets/MoveArrow.cs(24,53): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

回答

2

嘗試:

void Update() 
{ 
    var playerMapPos = GameObject.FindWithTag ("Player"); 
    var playerWorldPos = GameObject.FindWithTag ("PlayerCube"); 
    playerWorldPos.transform.rotation = playerMapPos.transform.rotation; 
} 

的原因,你正在試圖做的是不工作是transform.rotation是四元,而transform.eularAngles是Vector3。

+0

哈哈哈 - 謝謝!謝謝! - 對我來說很有趣,因爲當它如此簡單時,我只是「從一座小山上劃出一座山」。 :) :) 大聲笑 – Twitch 2015-04-02 18:42:42