- Home /
I am having an issue with moving a game object using MoveTowards.
I am in the latter stages of development for my 2D platform game and am currently working on a boss battle. I have previously used Vector3.MoveTowards for various things such as enemies and moving platforms and have had no issues but for some reason the movement for this object is just not working and I can't figure out why not. Every other part of the script works absolutely perfectly but the movement is just not happening. Could someone please help me to figure out what I must be missing? Scripts are added below.
First, the movement script
{
public Vector3 posA;
public Vector3 posB;
public Vector3 posC;
public float speed;
[SerializeField]
private Transform childTransform;
public bool movingright = false;
public bool movingleft = false;
public Vector3 currentpos;
public Vector3 nextpos;
private SpriteRenderer sidsprite;
public Animator sidanim;
public Transform[] waypoints = new Transform[3];
// Use this for initialization
void Start ()
{
currentpos = waypoints[0].localPosition;
nextpos = waypoints[1].localPosition;
posA = waypoints[0].localPosition;
posB = waypoints[1].localPosition;
posC = waypoints[2].localPosition;
sidsprite = GetComponentInChildren<SpriteRenderer>();
sidanim = GetComponentInChildren<Animator>();
}
public void FindNext()
{
if (currentpos == posA)
{
nextpos = posB;
Debug.Log("Next Position is " + nextpos);
}
if (currentpos == posB)
{
nextpos = posC;
Debug.Log("Next Position is " + nextpos);
}
if (currentpos == posC)
{
nextpos = posA;
Debug.Log("Next Position is " + nextpos);
}
}
public void Flip()
{
if (nextpos == posA | nextpos == posB)
{
sidsprite.flipX = true;
movingright = true;
}
else if (nextpos == posC)
{
sidsprite.flipX = false;
movingleft = true;
}
}
public IEnumerator Movements(Transform[] waypoints)
{
sidanim.SetBool("IsIdle", false);
sidanim.SetBool("IsMoving", true);
if (Vector3.Distance(currentpos, nextpos) >= 0.01f)
childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextpos, speed * Time.deltaTime);
if (Vector3.Distance(childTransform.localPosition, nextpos) < 0.01f)
{
movingright = false;
movingleft = false;
sidanim.SetBool("IsIdle", true);
sidanim.SetBool("IsMoving", false);
yield return new WaitForSeconds(1);
FindNext();
}
}
}
All of the relevant transforms and everything is added in the inspector and everything works apart from the character does not move. The animations happen and the wait times are correct and it finds the co-ordinates for the current and next positions as it should.
The movement is called through a separate script to activate the coroutine
public class StartBossRoutine : MonoBehaviour
{
private SidMovement move;
// Use this for initialization
void Start ()
{
move = FindObjectOfType<SidMovement>();
}
public IEnumerator StartMove()
{
move.sidanim.SetBool("IsIdle", true);
move.FindNext();
yield return new WaitForSeconds(2);
move.Flip();
StartCoroutine(move.Movements(move.waypoints));
Debug.Log("Should be moving");
}
}
This also works fine and the sprite flips at the correct time and the debug prints that the character should be moving but HE DOESN'T!!
Any assistance would be greatly appreciated.
Answer by Yoshinator2 · Jul 24, 2017 at 07:58 PM
The answer is simply that your MoveTowards is only being called once. the MoveTowards function does not constantly move the Vector3's toward each other without being called. Instead, each time the MoveTowards is called, it moves the vector closer to the target by the scale defined in the function. To fix this, just make it run the movetowards over and over until the object meets its target.
If this solved your problem, please accept my answer. If not, feel free to ask any more questions :)
Hi, thanks for the response, it makes perfect sense. However, I have spent the last few hours trying various different ways to do this and can't seem to get him to move at all. Usually I would just add a Boolean and set it to true to make the movement happen or just add the movement to Update but neither of these options seem to work. I am currently trying Invoke Repeating but I'm not 100% if I am using it correctly and it still isn't working! Could you please give me an idea as to how to implement your suggestion with code given as everything I keep trying seems to lead to me changing the code completely and not solving the issue?
Sure. Try something along the lines of:
public float speed;
public GameObject nextTarget;
private bool should$$anonymous$$ove = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (should$$anonymous$$ove)
{
//$$anonymous$$oves the position to the target
this.transform.position = Vector3.$$anonymous$$oveTowards(this.transform.position, nextTarget.transform.position, speed*Time.deltaTime);
//If the position has reached the target, this will run, stopping the function.
if(this.transform.position == nextTarget.transform.position)
{
should$$anonymous$$ove = false;
}
}
}
//Call this whenever you want to start the movement
public void start$$anonymous$$oving()
{
should$$anonymous$$ove = true;
}
Sorry I have only just got a chance to work on it again, I'll let you know shortly. Thanks again
EDIT: I still cannot him to move. I have tried using the script you gave me and still nothing and I have scaled it right back and removed the animator component but the character just won't move?!
Your answer
Follow this Question
Related Questions
MoveTowards won't translate on the y axis and lerp leaves character jittering in mid-air 0 Answers
My first game. Pong paddle question. 0 Answers
how to stop player from automatically jumping on a moving platform 0 Answers
Vector3.MoveTowards, y does not reach target 1 Answer
Error with moving object to mouse clicked coordinates. [SOLVED] 1 Answer