- Home /
How to make an object follow on 1 axis??
Howdy:
I'm taking a scripting class this semester and I need some help. I've been researching now for about half an hour and I can find a lot of questions that are sort of what I need, but not exactly, and I can't seem to successfully adapt any of the answers to what I'm working on.
I'm making a side scrolling "endless runner". It scrolls right to left and you control a character in the center that can move forward & backward 3 spaces and left & right 5 spaces (relative to the character). You are being chased by an enemy (both the enemy & the character are simple cubes at this point) that follows behind you and follows your movement along the Z coordinate.
I've kinda sorta got it working but I think there's a better solution out there and would like so suggestions. I've tried understanding Vector3.Lerp & Vector3.MoveTowards, but I can't make them do what I need them to do.
Let me preface all of this by saying that I just started working with Unity this semester so I'm a complete newbie at all this, and have limited programming skills (took an introductory C++ class last semester), so please bear that in mind when replying.
Here's the following cube's code that "sorta works":
#pragma strict
function Start ()
{
}
var IceKingSpeed = 20; // IceKing's movement speed
//var IceKingDelay = 0.5; // The amount of time (in seconds) the IceKing delays before following Finn
var zIceKing : float;
var IceKingzPosMoveLimit = 15;
var IceKingzNegMoveLimit = 3;
var IceKingKnockBack = 0.01;
var target : Transform;
var smooth = 5.0;
function Update ()
{
// Debug.Log("IceKing's z Position => [" + zIceKing + "] & IceKing's x Position => [" + xIceKing + "]"); // Shows the z position of Finn
// Basic Tracing Movement
zIceKing = transform.position.z;
var z = Input.GetAxis("Vertical") * Time.deltaTime * IceKingSpeed;
if (zIceKing <= IceKingzPosMoveLimit && zIceKing >= IceKingzNegMoveLimit)
{
// yield WaitForSeconds(IceKingDelay); ==> Never could this damn delay thing to work right, so I took it out.
transform.Translate(0, 0, z);
// transform.position.z = Vector3.Lerp (transform.position, target.position, Time.deltaTime * smooth); ==> I tried this but ERROR
}
else if (zIceKing > IceKingzPosMoveLimit)
transform.position.z -= IceKingKnockBack;
else
transform.position.z += IceKingKnockBack;
}
I'm open to suggestions, but please save the criticism if you have nothing constructive to offer, I'm trying to learn.
Answer by whydoidoit · Jul 31, 2012 at 12:04 AM
So you could do this:
Define the target position of the ice king as a script level variable
Change that position based on your input
Clamp the position
Lerp to that position for smooth movement
var iceKingTargetPosition : Vector3; var smoothingFactor : float = 4; //Smaller is more smooth var IceKingSpeed = 20; // IceKing's movement speed var IceKingzPosMoveLimit = 15; var IceKingzNegMoveLimit = 3; function Start() { iceKingTargetPosition = transform.position; } function Update() { iceKingTargetPosition.z = Mathf.Clamp(iceKingTargetPosition.z + Input.GetAxis("Vertical") * Time.deltaTime * IceKingSpeed, IceKingzNegMoveLimit, IceKingzPosMoveLimit); transform.position = Vector3.Lerp(transform.position, iceKingTargetPosition, Time.deltaTime * smoothingFactor); }
The only problem with this is that it will take a long time for the character to actually reach the target z (although it will close in quickly) as you are always moving towards it by a fraction.
You could also do a SmoothDamp - which will be more rapid to get there. The key to this technique is that the iceKingTargetPosition is a real world position.
var iceKingTargetPosition : Vector3;
var smoothingFactor : float = 0.5; //Smaller is less smooth
var IceKingSpeed = 20; // IceKing's movement speed
var IceKingzPosMoveLimit = 15;
var IceKingzNegMoveLimit = 3;
var IceKingVelocity : Vector3;
function Start() {
iceKingTargetPosition = transform.position;
}
function Update() {
iceKingTargetPosition.z = Mathf.Clamp(iceKingTargetPosition.z + Input.GetAxis("Vertical") * Time.deltaTime * IceKingSpeed,
IceKingzNegMoveLimit, IceKingzPosMoveLimit);
transform.position = Vector3.SmoothDamp(transform.position, iceKingTargetPosition, iceKingVelocity, smoothingFactor);
}