- Home /
Rigidbody slows down before touching its target
Hi, I am using a rigidbody and addForce to make an enemy move to the player, but when he is close, he slows down and moves too slowly to reach the player. I think it is because i calculate the distance between its position and the player's position, during a timestep, and if he comes too close, he only moves with a small distance :
But how should i do? I looked at the Angrybots project, but i have not seen something about that, maybe i misread the code.
Would you know how to avoid this issue? I would like the enemy to keep on rushing to the player until he touches him, not only until he gets close and then slows down.
This is the code :
if (hit.collider.name==player.collider.name){
print("ENEMY SEES PLAYER");
var tempRot = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToPlayer),Time.deltaTime);
tempRot.x=0;
tempRot.z=0;
transform.rotation=tempRot;
canSeePlayer=true;
}
}
// Apply a force that attempts to reach our target velocity
var velocity = rigidbody.velocity;
var targetVelocity = player.transform.position - transform.position;
var velocityChange = (targetVelocity - velocity);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.Force);
Thanks
Answer by Owen-Reynolds · Sep 03, 2012 at 10:52 PM
You are correct -- the problem is that `targetVelocity` depends on how far you are away. Not only does that make you creep when you get close, but enemies twice as far away move twice as fast. The trick is to turn targVel into a "standard length arrow" that points the way to move, not based on distance. Normalize does this. It makes any Vector3 be length 1, going the same direction.
In your code:
// old line, to get way to move:
var targetVelocity = player.transform.position - transform.position;
// new lines to make speed not depend on distance:
targetVelocity = targetVelocity.normalized; // Is now speed 1, going correct way
targetVelocity = targetVelocity * MOVE_SPEED; // pick move_speed
Of course, you can do that all in one line.
@Owen Reynolds thanks Owen, okay it did not really know what was normalized ;-) it is more clear now, but in my case, there is a problem, the enemy does not move, or it is barely visible, even if i set move_speed to "5000.0", would you know why?
@Owen Reynolds hi, actually it works fine, thanks, i don't know why it did not work before, maybe an issue with the "cache" in Unity, but i'm not sure how to fix this, i needed to restart it again and 24h after, it works fine. Thanks for your help
Hmm..."quit and restart" is such a common solution for other programs, but I've only had one problem where I needed to restart Unity (a script that accidentally deleted the built-in Cube mesh.)
Your answer
Follow this Question
Related Questions
Make characters go through each other 1 Answer
Calling Rigidbody.MovePosition, no movement at all 4 Answers
Rigidbody Version of RotateAround 2 Answers
Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 2 Answers
Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers