Search Unity

Basic move the cube with AddForce.

Discussion in 'Scripting' started by j2030658, Jun 17, 2018.

  1. j2030658

    j2030658

    Joined:
    Jun 14, 2018
    Posts:
    8
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EngineForward : MonoBehaviour {
    4.     public float thrust = 50000000.0f;
    5.     public Rigidbody rb;
    6.  
    7.     void Start() {
    8.         rb = GetComponent<Rigidbody>();
    9.     }
    10.  
    11.     void FixedUpdate() {
    12.         //Vector3 v3Force = thrust * transform.forward;
    13.         //rb.AddForce(v3Force);
    14.  
    15.         //rb.AddForce(transform.forward);
    16.  
    17.         //rb.AddForce(Vector3.up * thrust, ForceMode.Acceleration);
    18.     }
    19. }
    Three examples (each individual group; ripped from Google), no matter how high the thrust is set, it doesn't budge. I have a Rigidbody with gravity (which I need). The cube is 1 of mass, default settings.

    No budge whatsoever. How do I control the force behind rigidbody, how do I move it?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Here's a few points to check:
    1. The object must not be kinematic.
    2. Check the position constraints are not ticked.
    3. Is the FixedUpdate method actually being called?
    4. In the physics settings, is Auto Simulation set to true?
    5. When running, do other physics objects move?
    6. When running, if you drag this object into the air, does it fall under the force of gravity?
     
  3. j2030658

    j2030658

    Joined:
    Jun 14, 2018
    Posts:
    8
    1. Object is not kinematic.
    2. Constraints: position/rotation not locked.
    3. FixedUpdate() is standard Unity function, that should be used when dealing with Rigidbodies for more realistic physics simulation.
    4. No such option in PhysicsManager.
    5. Yes, I even dropped the cube itself from height and it behaves almost like a real object.
    6. Look 5. Yes. It does comply to physics.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    3. You are correct in your assertion about FixedUpdate but it will not be called if, for example, the script is inactive or not on any objects in the current scene. That was what my question was driving at. :)
    4. Yes there is : Edit => Project Settings => Physics ... then in the Inspector -> Auto Simulation. However, points 5 and 6 show this point is already covered.

    Ok, so how about trying this :-
    1. Add a brand new 3D cube to your scene ("TestCube").
    2. Add a rigidbody component to TestCube. Untick "Use Gravity".
    3. Add your script above to TestCube.
    What happens to TestCube? I have just tried this using your script and all 3 methods moved my TestCube. You should see the same results. If so, you will need to check what is different between TestCube and your actual game object.
     
  5. j2030658

    j2030658

    Joined:
    Jun 14, 2018
    Posts:
    8
    [snip]

    Nevermind, I'm just an idiot. I only had 5 on 'thrust', set it to 10000 thousand and it flew out of bounds, 100 was enough :p, sorry for wasting your time, and thank you for responding.
     
    Doug_B likes this.
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Nice. Glad you got it solved. :)
     
    j2030658 likes this.