After consulting several examples of how to do that, it doesn’t seem to work at all. The object just moves towards the indicated axis, but never gets back. Please help me, and suggest to me what I’m doing wrong. Thank you in advance!
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testing : MonoBehaviour
{
public int option;
public float speed;
private Vector3 target;
private Vector3 origin;
void Start(){
speed = 0.5f;
origin = transform.position;
}
void Update(){
if (option == 1)
{
target = new Vector3(1, 0, 0);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (transform.position == target)
{
target = origin;
}
Debug.Log("Selected option is " + option + ". Moving on the X axis.");
}
else if (option == 2)
{
target = new Vector3(0, 0, 1);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (transform.position == target)
{
target = origin;
}
Debug.Log("Selected option is " + option + ". Moving on the Z axis.");
}
else
{
Debug.Log("Selected option is " + option + ". There is no movement.");
}
}
}
Hey there,
the issue you have with your code is that you always overwrite the target
with the positions that you want to move to. So even if you reach the target position and you set target = origin
on the next Update run you reset target = Vector3(something, 0, someOtherThing)
.
So to solve this you should introduce a new option that you change to when moving back. To make this more readable and easier to understand i suggest you try to look into enumerations
. An enumeration can help you to differentiate different states that otherwise would be plain numbers:
public class testing : MonoBehaviour
{
//here we create an enumeration:
public enum OptionState {
None, // this is equal to "option 0"
MoveOnX, // this is equal to "option 1"
MoveOnZ, // this is equal to "option 2"
MoveToOrigin // and so on...
}
//our "option" is now not an integer but an "OptionState"
//you can always change between OptionState and int by casting from one to the other:
//int integerValueOfOption = (int)option;
//option = (OptionState)2; // <-different way to write "option = OptionState.MoveOnZ;"
public OptionState option;
public float speed;
private Vector3 target;
private Vector3 origin;
void Start()
{
speed = 0.5f;
origin = transform.position;
}
void Update()
{
if (option == OptionState.MoveOnX)
{
target = new Vector3(1, 0, 0);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (transform.position == target)
{
//we have reached the target so now we want to move back. For this we change the current state:
option = OptionState.MoveToOrigin;
}
Debug.Log("Selected option is " + option + ". Moving on the X axis.");
}
else if (option == OptionState.MoveTo2)
{
target = new Vector3(0, 0, 1);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (transform.position == target)
{
//we have reached the target so now we want to move back. For this we change the current state:
option = OptionState.MoveToOrigin;
}
Debug.Log("Selected option is " + option + ". Moving on the Z axis.");
}
else if (option == OptionState.MoveToOrigin)
{
//if we are in this state we move towards our origin:
transform.position = Vector3.MoveTowards(transform.position, origin, speed * Time.deltaTime);
if (transform.position == origin)
{
//now that we have reached the origin we set the state to "None" so nothing is done until we change the state again:
option = OptionState.None;
}
}
else
{
Debug.Log("Selected option is " + option + ". There is no movement.");
}
}
}
Let me know if something was unclear.
Edit: Same as Hellium answer this is not tested.
Code not tested
public float speed;
[Min(1)] public int option;
// Edit destinations in inspector
public Vector3[] destinations = new Vector3[]
{
new Vector3(1, 0, 0),
new Vector3(0, 0, 1)
};
private Vector3 target;
private Vector3 origin;
private Vector3 destination;
void Start(){
speed = 0.5f;
origin = transform.position;
if(destinations.Length > 0)
target = destinations[Mathf.Min(option, destinations.Length) - 1)];
else
target = origin;
}
void Update(){
if(destinations.Length > 0)
destination = destinations[Mathf.Min(option, destinations.Length) - 1)];
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if(transform.position == target)
{
if(target == destination)
target = origin;
else
target = destination; // Comment the else and this line if you don't want to loop
// Or more simply
// target = target == origin ? destination : origin;
}
}