Hi all,
having a strange issue with my code.
I just want my gameobject to move back and forth between x=10 and x=20.
it works the first back and forth, then it just begins to teleport between 10 and 20 and the cpu speed. Not sure what I aM doing wrong here...any help would be appreciated!
Thanks!
-Chris
using UnityEngine;
using System.Collections;
public class LurpScript : MonoBehaviour {
public float smoothmove = .5f;
float currentposition = 10;
float finalposition = 20;
Vector3 forwardPositon;
float revcurrentposition = 20;
float revfinalposition = 10;
Vector3 revforwardPositon;
public bool forRev = true;
void Start () {
}
void Update ()
{
if (transform.position.x < 10.5) {
forRev = true;
transform.position = new Vector3 (10, 0, 0);
}
if (transform.position.x > 19.5) {
forRev = false;
transform.position = new Vector3 (20, 0, 0);
}
if (forRev)
{
currentposition = Mathf.Lerp (currentposition, finalposition, smoothmove * Time.deltaTime);
transform.position = new Vector3 (currentposition, 0, 0);
}
if (!forRev)
{
revcurrentposition = Mathf.Lerp (revcurrentposition, revfinalposition, smoothmove * Time.deltaTime);
transform.position = new Vector3 (revcurrentposition, 0, 0);
}
having a strange issue with my code.
I just want my gameobject to move back and forth between x=10 and x=20.
it works the first back and forth, then it just begins to teleport between 10 and 20 and the cpu speed. Not sure what I aM doing wrong here...any help would be appreciated!
Thanks!
-Chris
using UnityEngine;
using System.Collections;
public class LurpScript : MonoBehaviour {
public float smoothmove = .5f;
float currentposition = 10;
float finalposition = 20;
Vector3 forwardPositon;
float revcurrentposition = 20;
float revfinalposition = 10;
Vector3 revforwardPositon;
public bool forRev = true;
void Start () {
}
void Update ()
{
if (transform.position.x < 10.5) {
forRev = true;
transform.position = new Vector3 (10, 0, 0);
}
if (transform.position.x > 19.5) {
forRev = false;
transform.position = new Vector3 (20, 0, 0);
}
if (forRev)
{
currentposition = Mathf.Lerp (currentposition, finalposition, smoothmove * Time.deltaTime);
transform.position = new Vector3 (currentposition, 0, 0);
}
if (!forRev)
{
revcurrentposition = Mathf.Lerp (revcurrentposition, revfinalposition, smoothmove * Time.deltaTime);
transform.position = new Vector3 (revcurrentposition, 0, 0);
}