I’m trying to make a GameObject move left and right, so I have the up and down code sorted. But in the following code it seems to set the new position and not move the GameObject itself.
I’ve been trying to work out what it could be but came to nothing.
This is the Code (It is a test script and there’s no other scripts fired from it):
public Vector3 StartLoc = new Vector3(0.0f,0.0f,0.0f);
public float MovementSpeed = 0.01f;
//Locations Vars
public Vector3 CurrentLocation;
public Vector3 NewLocation;
//Movement Bools
private bool AlreadyMoving = false;
private bool MoveUp = false;
private bool MoveDown = false;
private bool MoveLeft = false;
private bool MoveRight = false;
public GameObject SelectorGO;
// Use this for initialization
void Start () {
CurrentLocation = StartLoc;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.W) == true && AlreadyMoving == false)
{
NewLocation.z = CurrentLocation.z + 1.0f;
MoveUp = true;
AlreadyMoving = true;
}
if(Input.GetKeyDown (KeyCode.S) == true && AlreadyMoving == false)
{
NewLocation.z = CurrentLocation.z - 1.0f;
MoveDown = true;
AlreadyMoving = true;
}
if(Input.GetKeyDown (KeyCode.A) == true && AlreadyMoving == false)
{
NewLocation.x = CurrentLocation.x - 1.0f;
MoveLeft = true;
AlreadyMoving = true;
}
if(Input.GetKeyDown (KeyCode.D) == true && AlreadyMoving == false)
{
NewLocation.x = CurrentLocation.x + 1.0f;
MoveRight = true;
AlreadyMoving = true;
}
if(AlreadyMoving == true)
{
MovementCoding ();
}
}
void MovementCoding(){
if(MoveUp == true){
if(CurrentLocation.z >= NewLocation.z){
MoveUp = false;
AlreadyMoving = false;
CurrentLocation.z = NewLocation.z;
}
if(CurrentLocation.z < NewLocation.z){
CurrentLocation.z = CurrentLocation.z + MovementSpeed;
gameObject.transform.position = CurrentLocation;
}
}
if(MoveDown == true){
if(CurrentLocation.z <= NewLocation.z){
MoveDown = false;
AlreadyMoving = false;
CurrentLocation.z = NewLocation.z;
}
if(CurrentLocation.z > NewLocation.z){
CurrentLocation.z = CurrentLocation.z - MovementSpeed;
gameObject.transform.position = CurrentLocation;
}
}
if(MoveLeft == true){
if(CurrentLocation.x >= NewLocation.x){
MoveLeft = false;
AlreadyMoving = false;
CurrentLocation.x = NewLocation.x;
}
if(CurrentLocation.x < NewLocation.x){
CurrentLocation.x = CurrentLocation.x - MovementSpeed;
gameObject.transform.position = CurrentLocation;
Debug.Log(CurrentLocation.x)
}
}
if(MoveRight == true){
if(NewLocation.x <= CurrentLocation.x){
MoveRight = false;
AlreadyMoving = false;
CurrentLocation.x = NewLocation.x;
}
if(CurrentLocation.x > NewLocation.x){
CurrentLocation.x = CurrentLocation.x + MovementSpeed;
gameObject.transform.position = CurrentLocation;
Debug.Log(CurrentLocation.x)
}
}
}
Thanks very much.