Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Having trouble with adding force to an object with the transform forward of the other object

Discussion in 'Getting Started' started by Antoner_8, Nov 8, 2023.

  1. Antoner_8

    Antoner_8

    Joined:
    Aug 23, 2022
    Posts:
    10
    when the first object hits the other one, the other one uses oncollisionenter and addsForce impulse with the transform forward of the first object.

    Code (CSharp):
    1.         if (collision.collider.gameObject.CompareTag("FirstObject"))
    2.         {
    3.             Debug.Log(gameObject.name);
    4.             rb.AddForce(collision.collider.gameObject.transform.forward * punchImpulse, ForceMode.Impulse);
    5.         }
     
  2. Guardian865

    Guardian865

    Joined:
    Nov 9, 2023
    Posts:
    5
    what are you having trouble with this code? In my opinion, you code is correct. So what do you mean having trouble? You mean having trouble with detecting collision or not working on rigidbody add force?