- Home /
Moving a Rigidbody with .addforce is not working
Here is my code using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Fixedupdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
so this is not giving me any errors but i'm also not getting any control over my object it just sits there. also, is kenimatic and gravity are both unchecked.
any help would be great, this seems like it should work!
Use Debug.Log to find the values of your input, to see if its working as expected.
$$anonymous$$ake sure speed does not have a 0 value.
Also, you typically wont use force to move a character but rather use velocity. Force is usually added as a constant value for impacts and such.
Answer by DoTA_KAMIKADzE · Apr 07, 2015 at 10:31 PM
If IsKinematic is unchecked in rigidbody that code should work.
Also as people have mentioned in comments you should use:
rb.velocity = movement * speed;
Unless you want to constantly push your player and increase speed to enormous lighting fast with AddForce and that is what you expect.
P.S. Also make sure your speed is not zero.
If everything is fine for your eye then add this line of code at the bottom of your FixedUpdate():
Debug.Log(moveHorizontal + "//" + moveVertical + "//" + speed);
And post result here
P.P.S. Ah lulz just saw it:
you got Fixedupdate but it should be FixedUpdate )))
OH $$anonymous$$AN, thank you so much. also, that line of debug is super useful! Thanks again!
Your answer
Follow this Question
Related Questions
Physics in 2D mode not working? 0 Answers
ForceMode.VelocityChange vs. simply adding to .velocity 0 Answers
Basic Jump Pad help 2 Answers
rb.addforce being really buggy 2 Answers
Gameobject going past target when using rigidbody2d.addforce 1 Answer