Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved AddRelativeForce(transform.forward * -force) always adds force in same position globally

Discussion in 'Scripting' started by ArthurHaleta, Dec 31, 2021.

  1. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    Hey! So I wrote a function that basically makes the player shoot, so I add relative force to the bullet, to make it shoot correctly, and that works, but when I add relative force to the player for recoil, it always adds force in the exact same location globally. Here is the code:

    Code (CSharp):
    1. IEnumerator shoot()
    2.     {
    3.         bulletSpawn = objectToControl.transform.position + (transform.forward * 2);
    4.         GameObject spawnedBullet = Instantiate(bullet, bulletSpawn, Quaternion.identity);
    5.         attackLoading = true;
    6.         yield return new WaitForSeconds(0.75f);
    7.         attackLoading = false;
    8.         objectToControl.GetComponent<Rigidbody>().AddRelativeForce(objectToControl.transform.forward * -recoil);
    9.         spawnedBullet.GetComponent<Rigidbody>().AddRelativeForce(objectToControl.transform.forward * bulletForce);
    10.         yield return null;
    11.     }
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    ArthurHaleta likes this.
  3. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    Okay thanks! It works! Is there any like 'resolved' button I press or..?
     
    adamgolden likes this.
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    At the top right click "Thread Tools" then pick the green Resolved in the Edit Title thing :)
     
    ArthurHaleta likes this.