- Home /
Moving from point A to point B to point A to point B over and over
Basically I'm wanting a enemy to move back and forth from two points that I've assigned called point A and point B.
I've looked up over topics about this similar thing and none of them seemed to help so I thought I would create a new topic. The main thing is I'm using a enum to switch between the different states for the enemy and one of them is a nonAggro state which is when I want the enemy to move back and forth from point A and point B.
I also want to keep this PingPong motion going where the enemy moves up and down slightly, when it moves from point A to B and back to A as it doesn't when using things like lerp. Here is the PingPong script:
if(canMove)
{
transform.position = new Vector3(transform.position.x, yCenter + Mathf.PingPong(Time.time, 0.5f), transform.position.z);
}
Could anyone help?
Answer by Eno-Khaon · May 21, 2016 at 01:39 AM
You're on the right track, but there's a much more configurable way of implementing that:
public Vector3 pointA;
public Vector3 pointB;
public float repeatTime = 0.5f;
// ...
if(canMove && repeatTime > 0)
{
transform.position = Vector3.Lerp(pointA, pointB, Mathf.PingPong(Time.time, repeatTime) / repeatTime)
}
Using Linear Interpolation to go back and forth between points A and B (defined freely), The PingPong value can be scaled into a 0-1 range by dividing by its own upper limit.
Brilliant, It works though the PingPong that makes the enemy move up and down doesn't happen during it.
if(can$$anonymous$$ove)
{
transform.position = new Vector3(transform.position.x, yCenter + $$anonymous$$athf.PingPong(Time.time, 0.5f), transform.position.z);
}
This is in the FixedUpdate function and is meant to make the enemy go up and down all the time and I would like it to do this while moving which it does when I use something like Vector3.$$anonymous$$oveTowards but not with your script.
Not sure why :P
Conceptually, pointA and pointB would simply need to be defined.
To make them match your current code, it would look something like:
void Start()
{
pointA = new Vector3(transform.position.x, yCenter, transform.position.z);
pointB = pointA + (Vector3.up * 0.5f);
}
That should recreate the initial position (yCenter + 0) and destination position (yCenter + 0.5f) described in your code example.
Not exactly sure how to implement that.
Very new to using Vector3.lerp.
If you want to look at the related code then here:
$$anonymous$$aking enemy move up and down:
void FixedUpdate () {
if(can$$anonymous$$ove)
{
transform.position = new Vector3(transform.position.x, yCenter + $$anonymous$$athf.PingPong(Time.time, 0.5f), transform.position.z);
}
BossStates();
}
NonAggro State where I want the enemy to move back and forth but also move up and down caused by the above script:
case EyeBallStates.NonAggro:
NonAggro();
break;
void NonAggro()
{
if(anim.GetBool("Aggro"))
{
anim.SetBool("Aggro", false);
}
float repeatTime = 2.5f;
if(repeatTime > 0)
{
transform.position = Vector3.Lerp(movePoint1.transform.position, movePoint2.transform.position, $$anonymous$$athf.PingPong(Time.time, repeatTime) / repeatTime);
}
if(transform.position == movePoint1.transform.position)
{
transform.localScale = new Vector3(1, 1, 1);
}
else if(transform.position == movePoint2.transform.position)
{
transform.localScale = new Vector3(-1, 1, 1);
}
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
why does my movement function affect all objects in scene? 1 Answer
Character glides across floor 2 Answers