- Home /
Make Object Move to/follow Another Object? (plus turn towards it)
Hello.
I am trying to make a simple game where objects close in on an other object (you!).
I am having them spawn all around the character in a random location, so I will not be able to know where they are.
What I want to be able to do is have a script that will have a speed variable controlling how fast they come towards you.
I suck at scripting though, and I am even worse at scripting the movement of objects in 3D space.
I would also like the script to be compatible with following a character that moves. I was hoping some nice scripting guru could give me a script or lead me in the right path.
I saw something once a long time ago, a very complex Youtube tutorial, to doing what I want, but I can't find it plus it was in C# which I am trying to avoid, as I use Javascript.
I would really appreciate some help!
:P
Thanks!!
EDIT: I just remembered that the script will also need to have the object turn so it is always facing towards you. The objects are pieces of text, and I can't see some of them because they are either reversed or I am seeing the side.
2nd EDIT:
Thank you for your responce. Now I need a second script that will turn it forward facing the target. Anyone?
Thanks!
I can't seem to find out how to make the target. Could someone please get back to me?
could u please give me the link or the youtube tutorial ...?
Answer by Kobby · Apr 03, 2011 at 10:57 PM
Look in to the Vector3.moveTowards() function. it takes a current position, a target position and a float to determine the rate at which your object moves.
function Update(){
transform.position = Vector3.MoveTowards(transform.position, targ.transform.position, .03);
}
Also Lerping could do it for you
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
Hi. Thank you for your answer. The problem is that this script does not turn the gameobject to face forward towards the specified target. Any ideas on a second script to do that? Thanks!
sorry I forgot to respond to the edit. you can also call a transform.lookAt() that will keep your objects looking at 1 game object all the time.
private var targ: GameObject;
function Update(){ transform.LookAt(targ.transform); }
I amble to do the target follow thing. However, there are a number of other collider objects in between the target and the follower, and my follower(which has a collider component) is moving through those objects. How do I make him move around those objects ins$$anonymous$$d of through them?
Thank-you! You're the best! It worked perfectly for me. I multiplied my float speed by Time.deltaTime for a smoother performance.
Answer by dragen000 · Sep 18, 2015 at 12:36 PM
I have some thing similar i'm working on but i C# could you take a look at it. it doesn't seem to work. using UnityEngine; using System.Collections;
public class FollowAI : MonoBehaviour {
public GameObject target; //the enemy's target
public float moveSpeed = 5; //move speed
public float rotationSpeed = 5; //speed of turning
private Rigidbody rb;
private Vector3 mytransform = target;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
//rotate to look at the player
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
//move towards the player
transform.position += transform.forward * Time.deltaTime * moveSpeed;
}
}
I amble to do the target follow thing. However, there are a number of other collider objects in between the target and the follower, and my follower(which has a collider component) is moving through those objects. How do I make him move around those objects ins$$anonymous$$d of through them?
Your answer
Follow this Question
Related Questions
object follow mouse position in 3d world. 1 Answer
How to follow a moving object by camera on a line ? 1 Answer
Object where the mouse is 4 Answers
How to make a follow AI 4 Answers
Objects without Textures... 3 Answers