How do I move a 3d object from point a to b with a random timer?
I am trying to move an object from point a to point b (Same speed same position) but with a random timer between each move. I haven't been able to find anything about this specific scenario. I am completely new to coding also, so I can't piece odds and ends together, Please Help!
this question is actually asking multiple questions about coding. if you are trying to learn take one step at a time. you will eventually need to put things together your self. people here are reluctant to write entire codes for you. we more just help you when you are "stuck". Is your object moving yet? what do you have for code so far?
Answer by SohailBukhari · Mar 24, 2017 at 07:35 AM
class MyClass: MonoBehaviour
{
[SerializeField]private Vector3 _pointBVector3;
private void Start()
{
var pointAVector3 = this.transform.position;
StartCoroutine(MoveObject(this.transform,pointAVector3,_pointBVector3,Random.Range(2f,5f)));
}
/// <summary>
/// MoveObject
/// </summary>
/// <param name="_transform">Pass transform Which You Want To Move</param>
/// <param name="initialPos">Start Position Of Move Object</param>
/// <param name="endPos">PointB </param>
/// <param name="time">Random Time Like Random.Range(2f,5f)</param>
/// <returns></returns>
private IEnumerator MoveObject(Transform _transform,Vector3 initialPos,Vector3 endPos,float time)
{
var i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
_transform.position = Vector3.Lerp(a: initialPos, b: endPos, t: i);
yield return null;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612113833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move an object with a starting and ending speed and time 0 Answers
Increase spawn object movement when player collects every 10 points 0 Answers
Idea to know how to give realistic movements to a spaceship ( A sort of delay ) 0 Answers
How to move an object, depend to a second 0 Answers
How can I change the design of game_object at runtime? 0 Answers