Hey everyone I’m working on a simple 2d game, but I’ve run into a problem.
Right now if you shoot a projectile and it hits the wall it will bounce perfectly. But if it hits another wall it screws up, which is perfectly makes sense. Why? Because that’s how I coded it. Here is a example picture of the problem:
I need help to reformulate the code, and here it is:
public class bullet : MonoBehaviour {
public Vector3 velocity;
public float speed;
void Start () {
velocity = transform.up;
speed = 250;
StartCoroutine (Wait (5));
}
void Update () {
transform.position += velocity * Time.deltaTime * speed;
}
IEnumerator Wait(float seconds) {
yield return new WaitForSeconds(seconds);
Destroy(transform.gameObject);
}
public void OnCollisionEnter(Collision owner) {
if(owner.gameObject.tag == "wall") {
foreach( ContactPoint contact in owner.contacts ) {
velocity = Quaternion.AngleAxis(180, contact.normal) * transform.up * -1;
}
}
}
}