- Home /
Move Object A towards Object B at a definite speed
I want Object A to move towards Object B at a certain speed that is determined by the variable speed. I do also want it not to slow down upon nearing the target, or speed up when further from. It is to move only at a single, non-changing speed, and with the direction Object A is facing being of no consequense. If possible, answer so simply that a person who did not even understand the Unity Reference page can understand. Thank you.
Current script on ObjectA.
var speed = 4.0; var ObjectB:GameObject;
function Update () {
}
Please do not link me to reference pages. Because they are never tailored to fit my situation. Thank you.
Answer by Jean-Fabre · Apr 28, 2011 at 07:06 AM
Hi,
The easiest way out of it is to use iTween. You can roll your own of course.
Using the moveTo function you would end up with something like this:
Create a c sharp script and name it "ConstantSpeedMove", edit it and paste the script below.
using UnityEngine; using System.Collections;
public class ConstantSpeedMove: MonoBehaviour {
public Transform target;
public float speed = 1f;
void Start () {
if (target==null){
Debug.LogWarning("ConstantSpeedMove is missing a transform target");
}else{
iTween.MoveTo(gameObject,iTween.Hash(
"position",target.position,
"speed",speed,
"oncomplete","onCompleteFromiTween",
"easetype",iTween.EaseType.linear
));
}
}
void onCompleteFromiTween () {
Debug.Log("ConstantSpeedMove done");
}
}
Bye,
Jean
ps: Reference and help is never tailored to fit your exact need, rather they help you understand the function and feature so that you can then build it exactly how you want. Just felt the need to point that out :)
This is was what I need. Example: $$anonymous$$oving camera to target position using ease animation types. Thank you very much $$anonymous$$. You answer gave me better understanding how to use our custom variables when we decide to use iTween. Thank you again. How is going PUN2?
Your answer
Follow this Question
Related Questions
What am I doing wrong? 1 Answer
Move the player towards the cube to attack. 0 Answers
Why do my objects keep changing speed? 1 Answer
Moving an Object towards another Moving Object 2 Answers
How should I move an object that parents on collision? 1 Answer