Just cant get moveTowards vector3 to work.
Hi all,
I am trying to get an object to follow another as it randomly moves position. The problem is that the target (a cube) just blinks from position to position like it should, and the drone(a sphere) copies that behaviour, rather than actually travelling towards the target. i am using moveTowards method for this. here is the code, really appreciate any input on this one. Thanks in advance
public float positionX = 0f;
public float positionY = 0f;
public float positionZ = 0f;
private float speed = 1f; // move speed
public float delayTime = 0f;
public float executeTime = 0f;
private GameObject move_cube;
private Vector3 wayPointPos;
private GameObject drone;
// Use this for initialization
void Start () {
executeTime = Random.Range (0f, 4f);
move_cube = GameObject.Find("move_cube");
drone = GameObject.Find ("drone");
}
void Update () {
delayTime += Time.deltaTime;
if (delayTime >= executeTime) {
executeTime = Random.Range (0f, 4f);
delayTime = 0f;
positionX = Random.Range (0f, 5f);
positionY = Random.Range (0f, 5f);
positionZ = Random.Range (0f, 5f);
}
move_cube.transform.position = new Vector3 (positionX, positionY, positionZ);
wayPointPos = new Vector3 (move_cube.transform.position.x, move_cube.transform.position.y, move_cube.transform.position.z);
drone.transform.position = Vector3.MoveTowards(transform.position, wayPointPos, 1f * Time.deltaTime);
Answer by Cuttlas-U · Apr 14, 2017 at 07:22 PM
hi; use this :
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
this move your follower straight ahead so now u just need to make it look at your object to always follow it :
transform.LookAt(yourtarget);
Answer by neilgoss · Apr 14, 2017 at 10:29 PM
Hey Cuttlas-U
Thanks for that, it works great. Really appreciate it.
Your answer
Follow this Question
Related Questions
Vector3.MoveTowards Problems 0 Answers
How to create Curve between two points using **Vector3.MoveTowards**? 0 Answers
Vector3.MoveTowards 2 Answers
[Solved] Sliding garage door using transform.translate. 1 Answer
How to disable diagonal Vector3 movement of non-player object using MoveTowards? 1 Answer