Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Help with basic back and forth code

Help with basic back and forth code

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Help with basic back and forth code on Sat Jan 30, 2016 2:04 pm

xxtacoxx


Member [Lv. 0]
Member [Lv. 0]
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);

}

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum