Hi everyone, I have a script that changes the gameobject's direction when it reaches a point.For some reason when I set it for either the y or z axes they continue on forever.But if I set it in the x axis it moves back and forth like it suppose to.Any idea what is going on?
using UnityEngine;
using System.Collections;
public class ObjectXYZMovement : MonoBehaviour
{
public float pivot = 4;
public int speedInt = 2;
public Vector3 dir;
void XMovement(){
gameObject.transform.Translate(dir * speedInt * Time.deltaTime);
bool flipCondition = (dir.x < 0) ?
gameObject.transform.position.x < -pivot :
gameObject.transform.position.x > pivot;
if (flipCondition)
{
dir.x *= -1;
}
}
void YMovement(){
gameObject.transform.Translate(dir * speedInt * Time.deltaTime);
bool flipCondition = (dir.y < 0) ?
gameObject.transform.position.y < -pivot :
gameObject.transform.position.y > pivot;
if (flipCondition)
{
dir.y *= -1;
}
}
void ZMovement(){
gameObject.transform.Translate(dir * speedInt * Time.deltaTime);
bool flipCondition = (dir.z < 0) ?
gameObject.transform.position.z < -pivot :
gameObject.transform.position.z > pivot;
if (flipCondition)
{
dir.z *= -1;
}
}
void Update()
{
if(dir.x == 1 || dir.x == -1 ){
XMovement();
}
else if(dir.y == 1 || dir.y == -1 ){
YMovement();
}
else if(dir.z == 1 || dir.z == -1 ){
ZMovement();
}
}
}
↧