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
Share
Go down
Member [Lv. 0]
Member [Lv. 0]
Posts : 1
Points : 903
Rep : 0
Join date : 2016-01-30
View user profile

Help with basic back and forth code

on Sat Jan 30, 2016 2:04 pm
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);

}
Back to top
Permissions in this forum:
You cannot reply to topics in this forum