- Home /
c# wait script
Hi,
I am looking to make an object lerp left and right across the screen. i have the object lerping from one point to another but i need it to repeat from moving from right to left and back to right and so on. What is the best way to do this and how can you make it wait 1sec before it moves. if you need anymore info let me know. thanks for the help.
using UnityEngine;
using System.Collections;
public class move : $$anonymous$$onoBehaviour {
public float smooth;
private Vector3 newPosition;
// Use this for initialization
void Start () {
newPosition = transform.position;
}
// Update is called once per frame
void Update () {
Vector3 positionA = new Vector3 (3, 2, 0);
Vector3 positionB = new Vector3 (2, 2, 0);
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
}
Answer by bubzy · Nov 09, 2014 at 04:50 PM
timers have been covered so many times. a simple search should yield good results.
but heres a little script just for you :D
using UnityEngine;
using System.Collections;
public class moveWait : MonoBehaviour {
float moveDelay = 1f;
float smooth = 0.1f;
float currentDelay;
bool canMove = true;
bool moveToA = true;
Vector3 positionA = new Vector3(3,2,0);
Vector3 positionB = new Vector3(2,2,0);
void Start()
{
currentDelay = Time.time + moveDelay;
}
void Update()
{
if(canMove)
{
if(transform.position != positionA && moveToA)
{
moveToPosition(positionA); //start moving;
}
if(transform.position == positionA && moveToA)
{
moveToA = false;
canMove = false;
currentDelay = Time.time + moveDelay;
}
if(transform.position != positionB && !moveToA)
{
moveToPosition(positionB); //start moving;
}
if(transform.position == positionB && !moveToA)
{
moveToA = true;
canMove = false;
currentDelay = Time.time + moveDelay;
}
}
if(!canMove && Time.time > currentDelay)
{
canMove = true;
currentDelay = Time.time + moveDelay;
}
}
void moveToPosition(Vector3 target)
{
transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime);
}
}
bubzy thanks for the help. it is working but it is a small get slow. it moves fast at first and then it slows down and slowley moves to 3,2,0 how can you make it to get to its position in one smooth swipe and not slow down just before it gets to its 1st and 2nd position. thanks again!!
Answer by Vederant · Nov 09, 2014 at 04:56 PM
void Update () {
StartCoroutine(wait());
}
public IEnumerator move () {
yield return new WaitForSeconds (1.0f); //this stops the coroutine for 1 second.
Vector3 positionA = new Vector3 (3, 2, 0);
Vector3 positionB = new Vector3 (2, 2, 0);
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
I hope this helped.
this isnt switching from positionA to positionB and you aren't calling the move() function.