- Home /
Linear interpolating (Lerping) a Gameobject between random positions of Other GameObjects
I want to interpolate A Game Object between random positions of Other Game Objects. Just Trying to get it take cover in random spots in the game. I've tried this but it only moves one tick.
using UnityEngine;
using System.Collections;
public class CoverIdentification : MonoBehaviour {
private GameObject[] cover = new GameObject[5];
private int n;
private bool switcher = false;
// Use this for initialization
void Start () {
cover = GameObject.FindGameObjectsWithTag ("cover");
}
void Update()
{
StartCoroutine (switching ());
}
IEnumerator switching()
{
if(switcher == false)
{
switcher = true;
n = Random.Range (0, cover.Length);
Debug.Log (n);
yield return new WaitForSeconds (3f);
transform.position = Vector3.Lerp (gameObject.transform.position, cover [n].transform.position,Time.deltaTime);
yield return new WaitForSeconds (3f);
switcher = false;
}
}
}
Answer by Mister-Mortal · Nov 22, 2016 at 07:00 PM
That is because you are calling Lerp method only once. Lerp is a simple method that returns one value after being called.
In my opinion using iTween plugin would achieve your desired effect with no effort.
With the plugin instead of using Lerp you could just do this:
iTween.MoveTo(gameObject, cover[n].transform.position, movementDuration);
This would animate your game object to your desired location with no extra code.
@Ashherr01 Good to hear! If the solution was helpful don't forget to mark it as a best answer, so others with the same problem could find the answer ;)
Your answer
Follow this Question
Related Questions
Vector3 Lerp mis-match 0 Answers
C# Array with random.range help? 2 Answers
C# 2d Instantiate object randomly around gameobject 1 Answer
what to put in vector3 when instantiating Gameobject at Gameobject? 2 Answers
Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer