- Home /
Moving character from point to point
Hey Unity Heroes,
I've been searching and searching for a way to move a character from point to point, but I can't seem to figure out how to actually get it done. I've seen it done by Lerping and moving back and forth between a position, but I just can't seem to be able to move an object from its starting position downward and then stay at that other position. Basically, what I have is a boss character that spawns at the top of the "playing field," which is off screen (my game is 2D btw) and then the boss is supposed to move downward slowly to a point and stay there so that the player can fight this boss. I'll post a screenshot of where the boss is spawning and then I'll post the code that I have for the boss movement. (EDIT) - The transform.position.x that you see is because I'm moving the boss back and forth on the X axis.
#pragma strict
//var target : GameObject;
var pointA = Vector3(0, 15, 178);
var pointB = Vector3(0, 0, 178);
//private var startPosition : Vector3;
function Start () {
//transform.position = Vector3.Lerp(
//transform.position = Vector3.Lerp(startPosition, target.transform.position, Time.deltaTime * 5);
}
function FixedUpdate () {
transform.position.x = (Mathf.PingPong(Time.time * 3, 5) - 3);
transform.position = Vector3(transform.position.x, transform.position.y, transform.position.z);
}
![alt text][1] [1]: /storage/temp/24783-screen+shot+2014-04-05+at+12.03.24+pm.png
From the image you can see that the boss is spawned outside of the playing area. I want the boss to move downward slowly, to about where you can see the Pink and Blue objects, and then stop. Any help is greatly appreciated and if you need any further clarification, please do not hesitate to ask.
Answer by sumeetkhobare · Apr 05, 2014 at 07:59 PM
var hasFallenDown : boolean = false;
var timeCount : float = 0;
function Update()
{
if(!hasFallenDown)
{
transform.position = Vector3.Lerp(pointA,pointB,timeCount);
timeCount += Time.deltaTime * 5;
if(timeCount >= 1.0)
hasFallenDown = true;
}
}
Try this code... mark as solve if it solves the problem.
wow! thank you very much! This totally worked! $$anonymous$$uch appreciated! With some tweaking, I can get this fixed up more in no time.
Your answer
Follow this Question
Related Questions
Enemy ia fliping when hitting object 2 Answers
Problem with the movement of AI 1 Answer
How To Deactivate Enemy Shooting 1 Answer
Enemy not rotating when inside range 1 Answer
Enemy rotation issues - Javascript 0 Answers