I'm trying to learn Unity. My goal is to build a little asteroids clone.
Currently, my character is able to fly, shoot and rotate towards the mouse location. However, the bullets are somehow effected by the player turning.
Here is some sample code:
player.cs
private void Shoot()
{
Instantiate(Bullet, transform);
}
private void FaceMouseDirection()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.back);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turningSpeed * Time.deltaTime);
}
bullet.cs
private GameObject parent;
private Vector3 Force;
// Use this for initialization
void Start ()
{
parent = GameObject.Find("Player");
Force = parent.transform.up * bulletSpeed;
}
void FixedUpdate()
{
transform.Translate(Force * Time.fixedDeltaTime);
}
}