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

AddForce not working

Discussion in 'Physics' started by EchiGames, Jun 20, 2017.

  1. EchiGames

    EchiGames

    Joined:
    May 28, 2017
    Posts:
    1
    I have this following code in a FixedUpdate() method with a Input.GetMouseButton(0) condition:

    Code (CSharp):
    1. public void playerAttack()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.Raycast(player.transform.position, player.transform.forward, out hit, range)) //range = 7f
    5.         {
    6.             if (hit.rigidbody != null && hit.transform.tag == "Enemy")
    7.             {
    8.                 Vector3 dir = new Vector3(hit.transform.position.x, hit.transform.position.y, hit.transform.position.z - 100f);
    9.                 hit.transform.GetComponent<Rigidbody>().AddForce(dir * weaponPush * Time.deltaTime); //weaponPush = 1f
    10.             }
    11.         }
    12.  
    13.     }
    The enemy object has rigidbody on it and isKinematic is not selected. It still doesn't move when I am almost in front of it and click the left mouse button.
     
  2. BamBamAlicious

    BamBamAlicious

    Joined:
    Jan 29, 2017
    Posts:
    58
    I would guess in FixedUpdate() you are calling playerAttack() every frame?
    The problem I can see is that your program may be running at 1000fps, yet fixedUpdate is possibly only running at 60fps. Try moving the method involving the Input.GetMouseButton to update and see if it works then!