- Home /
I'd like to make my object chase another one of my objects.
Hello everyone,
I have just tried to do just the one function: There is one target object. I am moving the object in runtime.
There's another object which should chase the aforementioned object.
It should, let us say colourfully, "catch" the target object if the objects touch. If caught, the target object, she will be completely destroyed with a dramatic flourish. That's the concept in a nutshell.
But now .. as so often in life ... the problem...
My problem, the hunted object is moving but the hunter-object is not moving correctly.
Can anyone help me do this? Thanks!
Without posting the script you are having trouble with, there won't be much help. Code is like lego : the bricks are standard, but what a person makes with them is pretty much unique.
@alucardj is right: we should see your script in order to help you (our telepathic powers aren't good enough to visualize your script!)
Answer by aldonaletto · Jun 25, 2012 at 01:15 PM
You should post your script - how could we help without seeing it? Anyway, the basic idea is: calculate the target direction and move towards it - something like this:
var target: Transform; // drag the target here, or "GameObject.Find" it at Start var chaseSpeed: float = 5;
function Update(){ var dir = target.position - transform.position; // get the target direction var dir.y = 0; // keep it strictly horizontal var vel = dir.normalized chaseSpeed; // calculate the velocity vector // the way you will actually move depends on your character's nature: //- If it's a simple object (no Rigidbody, no CharacterController): transform.Translate(vel Time.deltaTime, Space.World); //- If it's a CharacterController (SimpleMove includes gravity automatically): GetComponent(CharacterController).SimpleMove(vel); //- If it's a Rigidbody (not recommended: rigidbodies are too wild!): vel.y = rigidbody.velocity.y; // keep the rigidbody gravity rigidbody.velocity = vel; }
I was just typing up a transform.LookAt(target) and transform.position += transform.forward x speed x Time.deltaTime , but you have given much more helpful info =]
Your answer
![](https://koobas.hobune.stream/wayback/20220613073239im_/https://answers.unity.com/themes/thub/images/avi.jpg)