- Home /
AddForce not working
I am currently using this script to make a monster chase the player:
#pragma strict
var target : Transform;
var moveSpeed = 240;
var rotationSpeed = 180;
var myTransform : Transform;
var myRigidbody : Rigidbody;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
myRigidbody = rigidbody;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myRigidbody.AddForce(myTransform.forward * moveSpeed * Time.deltaTime);
}
For some reason though the monster doesn't actually move. It turns to look at the player but doesn't chase. Does anyone know the reason?
Three things to check out:
Is your rigidbody marked as kinematic? If so it will not move.
Did you constrain movements in the rigidbody?
Is your rigidbody colliding with anything? Add an OnCollisionEnter() with a Debug.Log().
The rigidbody is not kinematic, no movements are constrained, and it isn't colliding with anything.
I just tested your code, and it worked fine for me. Note that it requires that you have a game object in the scene (other this this one) tagged "Player" for this code to work. You may also want to turn gravity off. I suggest a few other things. Any variable that you do not want to change in the inspector be made private. 'moveSpeed' should really be 'moveForce' since the value is not directly related to the speed of the object. Also applying force repeatedly is better done in FixedUpdate() (and therefore will not need to be scaled by Time.deltaTime). So the fixing these problems would make the code:
#pragma strict
var moveForce = 24;
var rotationSpeed = 180;
private var target : Transform;
private var myTransform : Transform;
private var myRigidbody : Rigidbody;
function Awake() {
myTransform = transform;
myRigidbody = rigidbody;
}
function Start() {
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
function FixedUpdate() {
myRigidbody.AddForce(myTransform.forward * moveForce);
}
But again, your original code worked for me.
It still doesn't want to move. It's probably something in the level like the floor or the roof then since when I used transform to move it, it worked fine (probably due to ignoring the physics of whatever it is colliding with now).
I've now got it working. I just added a box collider as well and he now follows the player without going through the walls. Thanks for the help!
Your answer
Follow this Question
Related Questions
Declaring myRigidbody 1 Answer
How do I add force To rigidbody based On Character's Walking direction? 0 Answers
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
Erratic Physics behaviour while testing in 2 computers 0 Answers
Make a player jump tile 1 Answer