2015-06-09 173 views
0

所以我得到了这个代码来实例化我的游戏对象加农炮,女巫有一个小孩“球”附加到它。当我使用我的左/右箭头键时,我试图旋转“大炮”周围的“球”。旋转一个带有子对象的游戏对象(Unity 2D)

using UnityEngine; 
using System.Collections; 

public class NewBehaviourScript : MonoBehaviour { 

    public GameObject Cannon = null; 
    public float speed = 1.0f; 

    void Start() { 
     Cannon = Instantiate (Resources.Load ("Prefabs/Cannon")) as GameObject; 
    } 

    // Update is called once per frame 
    void Update() { 

     if (Input.GetKey(KeyCode.LeftArrow)){ 
      Cannon.transform.Rotate(Vector3.left * speed * Time.deltaTime); 
     } 
     if (Input.GetKey(KeyCode.RightArrow)){ 
      Cannon.transform.Rotate(Vector3.right * speed * Time.deltaTime); 
     } 
    } 
} 
+0

但是你遇到的问题是什么? –

回答

3

没有Vector3.left,这将是Vector3.right的逆,因此您只需将其写为“-Vector3.right”即可。但无论如何,你不应该通过你想旋转的方向vector3在,但你想旋转的轴。在这种情况下,您可以将Vector3.up用于两个参数,并且使用“* speed”作为一个参数,而使用“* -speed”作为另一个参数。

+0

实际上有,不确定这是否因为您发布您的回应或更改发生了变化:https://docs.unity3d.com/ScriptReference/Vector3-left.html – KenSchnetz