help with block moving code
Hi all,
Just trying a simple task of moving a block back and fourth and a strange issue arrises.
The block moves from x = 10 to X = 20, then returns from 20 to 10 without and issue.
After that, it continuously teleports from10 to 20 over and over again.
Any ideas?
Thanks!!!
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 < 11)
forRev = true;
if (transform.position.x > 19)
forRev = false;
if (forRev)
{
currentposition = Mathf.Lerp (currentposition, finalposition, smoothmove * Time.deltaTime);
forwardPositon = new Vector3 (currentposition, 0, 0);
GetComponent<Rigidbody> ().MovePosition (forwardPositon);
}
if (!forRev)
{
revcurrentposition = Mathf.Lerp (revcurrentposition, revfinalposition, smoothmove * Time.deltaTime);
revforwardPositon = new Vector3 (revcurrentposition, 0, 0);
GetComponent<Rigidbody> ().MovePosition (revforwardPositon);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612062336im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
error CS0120: An object reference is required to access non-static member `ScoreManager.score' 1 Answer
How to make an enemy 2d chasing player? 1 Answer
Wrong after Update? 0 Answers
warning CS0618 with unity ngui 0 Answers
Same Q as before but it has been a while and nones replyed so im reposting 2 Answers