- Home /
Duplicate Question/Multiple Issues
How do i make an object move unpredictably?
I'm trying to get an object to move from one position to another and as soon as it reaches that position, another position to get randomly picked and for the object to move to THAT one till it gets clicked on. If there's a solution for this, preferably in C# that'd be lovely. Thank you in advance.
This is what i got so far:
using UnityEngine;
using System.Collections;
public class MoveGreg : MonoBehaviour {
public float movementSpeed;
public Vector3 direction;
void Update(){
while (true) {
direction = new Vector3(random(-10,10),random (-4.59,4.56),0);
movementSpeed = random(0.1,0.8);
transform.Translate(direction * movementSpeed * Time.deltaTime);
}
}
public float random(double min, double max)
{
return Random.Range ((float)min, (float)max);
}
}
Unity Answers is for answering single specific technical questions. Your question has multiple issues:
Finding a random position: you don't say what kind of position. It could be in a square area, a cubic area, within a sphere. All of these have been covered on UA. Exa$$anonymous$$e the various functions and the sample source for Unity's Random class.
$$anonymous$$ove a block over time: Your code is not the right way to move an object over time. $$anonymous$$oving object over time is in the top ten list of most asked questions. There are 1000's of answers on UA. Search out these answers. For your purposes, take a look at Vector3.$$anonymous$$oveTowards() and Vector3.Lerp() in the Unity reference.
Detect a click on an object: There's Raycasting() and On$$anonymous$$ouseDown() to solve this problem. Raycasting is also in the UA top ten. You will find 1000's of answers.
Break your problem into these three parts. Write test apps to solve each one independently, then put the three together. If you get stuck, we welcome your new question with a single, specific issue.
Follow this Question
Related Questions
Moving an Object - The right way. 2D 1 Answer
Instantiated object isnt moving 1 Answer
Problem with a motion script 0 Answers
Sum Of Two Motions? 0 Answers