Search Unity

Rigidbody/AddForce not moving the object.

Discussion in 'Scripting' started by arrowman888, Jul 10, 2018.

  1. arrowman888

    arrowman888

    Joined:
    Mar 13, 2015
    Posts:
    6
    Hey everyone!
    So I've recently been working on a project and just as I was getting into the scripting side I already came into a problem... Whenever I add a force to the object, it won't move. No matter how hard I try the object refuses to move. I've had no error messages, and I think my code is correct, if you see any problems please help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveRocket : MonoBehaviour {
    6.  
    7.     public GameObject rocket;
    8.     public float moveSpeed = 1000f;
    9.     private Rigidbody rigidbodys;
    10.  
    11.  
    12.     void Start () {
    13.         rigidbodys = rocket.GetComponent<Rigidbody> ();
    14.     }
    15.    
    16.  
    17.     void FixedUpdate () {
    18.         if (Input.GetKey (KeyCode.W)) {
    19.             rigidbodys.AddForce (0, moveSpeed * Time.deltaTime, 0);
    20.         }
    21.     }
    22. }
    23.  
    I don't know what's wrong any help is appreciated,
    Thanks!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What are your mass and drag settings for that rigidbody? Have you verified that AddForce is actually getting called?
     
    arrowman888 likes this.
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    The issue is most probably the mass of the rigidbody. Try the default value of 1 and see if that helps.
     
    arrowman888 likes this.
  4. arrowman888

    arrowman888

    Joined:
    Mar 13, 2015
    Posts:
    6
    Yes i have verified that it is being called and the settings for the rigidbody has nothing changed except for it is kinematic
     
  5. arrowman888

    arrowman888

    Joined:
    Mar 13, 2015
    Posts:
    6
    Everything is the same except its kinematic and is interpolated i have tried al combinations with those but haven't changed anything else
     
  6. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    602
    You said it is kinematic, right? That is why. Kinematic objects do not use the physics system. Either uncheck isKinematic or use transform.position.MoveTowards (or one of the other methods that moves the transform rather than rigidbody). But remember that the transform ignores physics, and moving using the transform like this would not trigger/collider colliders in the way you'd expect.
     
  7. arrowman888

    arrowman888

    Joined:
    Mar 13, 2015
    Posts:
    6
    Oh my lord thanks so much for this... I knew it was such a simple fix but i totally forgot about turning that on and off. You are a life saver. Gold medal. Thanks again.
     
    tsibiski likes this.
  8. HernandoNJ

    HernandoNJ

    Joined:
    May 13, 2018
    Posts:
    75
    I was using this code with a 3d cube and a 2d gameobject, Game view in 2d
    Code (CSharp):
    1. spawnedWeapon.AddForce(spawnedWeapon.transform.forward * fireForce);
    1. in 3d view, cube was moving forward, but in 2d it will appear to be in the same place
    2. a 2d gameobject can't move in z axis

    Hope it helps.
     
  9. HernandoNJ

    HernandoNJ

    Joined:
    May 13, 2018
    Posts:
    75