0

I'm trying to develop a simple shooting mechanism which instantiates a 'shot' based on the angle of rotation of the shooter. My problem here is even though the shooter is oriented at 0*, the shot fires at an angle of 45*(this is what I'm guessing the problem is because when the shooter is oriented at 45*, the shot is fired at exact 90*).

Shooter angle(0,0,0)

enter image description here

Shooter angle(0,0,45)

enter image description here

Note- The ball always launches from the center of the black flattened cylinder object.

Required Code:

public class ShotMoveScript : MonoBehaviour {
    public static float xForce;
    public Transform shott;
    void Update () {
        if(Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(Vector3.forward, 5f);
        }

        if(Input.GetKey(KeyCode.E))
        {
            transform.Rotate(Vector3.forward, -5f);
        }

        if(Input.GetKey(KeyCode.Space))
        {
            xForce += 0.2f;
        }

        if(Input.GetKeyUp(KeyCode.Space))
        {
            Instantiate(shott, transform.position, transform.rotation);
        }

    }
}

Script attached to the ball which gets instantiated:

public class MovementScript : MonoBehaviour {
    void Update () {
        Rigidbody2D rb;
        rb = GetComponent<Rigidbody2D> ();
        rb.gravityScale = 0.8f;
        transform.Translate( new Vector3(1,1,0) * ShotMoveScript.xForce * Time.deltaTime, Space.Self);
    }
}
4

1 回答 1

1

在使用第三个参数旋转实例化它时旋转球(参见参考资料)。之后,您应用一个指向 (1, 1) 的向量,这样球将沿着相对于他的局部旋转的方向移动。

要么将Quaternion.identity作为第三个参数传递给Instantiate方法,要么将球沿 (1, 0) 方向移动。

于 2015-10-06T12:00:43.117 回答