简体   繁体   English

旋转带有附加子项的GameObject(Unity 2D)

[英]Rotate a GameObject with a child attached to it (Unity 2D)

So I got this code to instantiate my gameobject Cannon, witch has a child "ball" attached to it. 因此,我获得了此代码来实例化我的游戏对象Cannon,女巫附加了一个孩子“球”。 I'm trying to rotate the "ball" around the "cannon" when I use my left/right arrow keys. 当我使用向左/向右箭头键时,我试图围绕“大炮”旋转“球”。

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);
        }
    }
}

There is no Vector3.left, that would be an inverse of Vector3.right so you would just write that as "-Vector3.right". 没有Vector3.left,它与Vector3.right相反,因此您只需将其写为“ -Vector3.right”即可。 But regardless, you shouldnt be passing the vector3 of the direction you want to rotate in , but the axis in which you want to rotate around . 但无论如何,你不应该被传递要旋转方向的Vector3,但轴要 in this case, you would use Vector3.up for both arguments, and use "* speed" for one, and "* -speed" for the other. 在这种情况下,您将对两个参数都使用Vector3.up,对一个参数使用“ * speed”,对另一个参数使用“ * -speed”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM