Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    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:
    29
    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. polemical

    polemical

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

    ArthurHaleta

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

    polemical

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