Unity2D bulletmovement [closed]

3 weeks ago 18
ARTICLE AD BOX

I'm new to Unity and C# and tried coding a bullet.

I want to know why this doenst work (all the floats have values and all the GameObjects are assigned to things)

I also feel like this code is very suboptimal because of the sin/cos

Thanks for helping out

using UnityEngine; public class BulletScript : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { Vector2 playerdirection = target.transform.position - transform.position; float playerdirectionrad = Mathf.Atan2(playerdirection.y, playerdirection.x); float angledirection = Mathf.Atan2(playerdirection.y, playerdirection.x) * Mathf.Rad2Deg; Vector3 bulletmovement = new Vector3(bulletspeed * Mathf.Cos(playerdirectionrad), bulletspeed * Mathf.Sin(playerdirectionrad), 0); } public GameObject target; public float bulletspeed; private Vector3 bulletmovement; // Update is called once per frame void Update() { transform.position += bulletmovement * Time.deltaTime; } }
Read Entire Article