- Home /
Chasing Object Sinking
Hi,
I have an OBJ made in MakeHuman with a sphere collider that I want to follow my player when I come within a certain distance. I was helped out with a script that does this well. However my sphere which is inside my OBJ (a human) heads toward my character, but converges towards my FPS' feet, and in doing so, the feet of the object chasing me sink through the ground.
I have tried Rigidbodies, various colliders, character controllers all to no avail. Below is my script, attached to the OBJ. I suppose what I want is to fix the Y axis on the OBJ so that it stays at the same height. Either that or solidify the OBJ.
// This is the object to follow
var leader : Transform;
// This is the object which follows
var follower : Transform;
// This is the speed with which the follower will pursue
var speed : float = 2.0;
// This is the range at which to pursue
var chaseRange : float = 10.0;
// This is used to store the distance between the two objects.
private var range : float;
function Update(){
// Calculate the distance between the follower and the leader.
range = Vector3.Distance( follower.position,leader.position );
if ( range <= chaseRange ){
// If the follower is close enough to the leader, then chase!
follower.LookAt(leader);
follower.Translate( speed * Vector3.forward * Time.deltaTime);
} // End if (range <= chaseRange)
else {
// The follower is out of range. Do nothing.
return;
} // End else (if ( range <= chaseRange ))
} // End function Update()
Answer by TheDarkVoid · Dec 10, 2012 at 05:57 PM
I think you should add an empty gameObject to your character that is higher and set that as te leader
Thanks man that worked perfect, just stuck a sphere above the camera and set that as the leader.
Appreciate the help.
How to add bouncy effect to this following object while it collide with other game objects (other then your player).
Answer by Jake.OConnor · Dec 10, 2012 at 06:12 PM
It would appear that the center of your "leader" is at the foot of the object. When you call LookAt it is orienting the follower towards whatever the center of leader is and then you're heading towards it. Highlight the "leader" in the inspector and look at where the position gizmo shows up, this is where the follower is going to be pointing towards. If it is at the foot then that is the problem, and you can add an empty dummy GameObject to your leader's heirarchy to denote the center. Once you have that, use that in the LookAt() function.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085234im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem using boids algorithm for chasing enemies. 1 Answer
Getting the player to retreat a certain distance from the player 1 Answer
Enemy AI Problem 0 Answers
Solid enemy help 1 Answer
Enemy raycast chase player 0 Answers