mercredi 30 août 2017

Unity reflecting bullets

I am trying to reflect my bullets that I fire, like light would but currently the bullets just go to the slide along and then carry on in the same direction.

could it be any project settings I am using?

here is my code:

public float speed;
private Vector3 oldVelocity;

private void Start()
{
    Rigidbody rigidbodyTemp = GetComponent<Rigidbody>();
    rigidbodyTemp.freezeRotation = true;
    rigidbodyTemp.isKinematic = false;
    rigidbodyTemp.detectCollisions = true;

}

// Update is called once per frame


// Update is called once per frame
void FixedUpdate () {
    Rigidbody rigidbodyTemp = GetComponent<Rigidbody>();

    transform.Translate(Vector3.forward * speed * Time.deltaTime);
    oldVelocity = rigidbodyTemp.velocity;


}

private void OnCollisionEnter(Collision collision)
{
    Rigidbody rigidbodyTemp = GetComponent<Rigidbody>();

    // Used for contact point
    ContactPoint contact = collision.contacts[0];

    Vector3 reflectedVelocity = Vector3.Reflect(oldVelocity, contact.normal);

    rigidbodyTemp.velocity = reflectedVelocity;
    Quaternion rotation = Quaternion.FromToRotation(oldVelocity, reflectedVelocity);
    transform.rotation = rotation * transform.rotation;

    if(collision.gameObject.tag == "Box")
    {

    }
}

}





Aucun commentaire:

Enregistrer un commentaire