Question by
n-carlson1 · Aug 07, 2016 at 02:23 PM ·
gameobjectpositionrandom
How can I find a new position once the old one is found?
I want to find a new position once the old one is found and I'm not sure how to check for that.
This is kind of what i've thought of so far.
void Start ()
{
transform.position = new Vector3(0, 2, -4);
}
// Update is called once per frame
void Update ()
{
//check if position is found here or something.
}
void FindNewPosition() //might need to do an output parameter?
{
Vector3 newPosition = new Vector3(Random.Range(-5.0F, 5.0F), 2f, Random.Range(-5.0F, 5.0F));
}
}
Comment
Answer by Masterio · Aug 07, 2016 at 04:13 PM
if( (transform.position - targetPosition).magnitude < 0.01f )
{
// transform is on the target position
// do something with this ....
}
else
{
// transform is moving to the target position
// example of move
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed);
}
Place it in Update.
That doesn't define what the target position is though and how those statements can find it.
Your answer
Follow this Question
Related Questions
CS0120 - Object reference required but it's already assigned. 0 Answers
comparing two gameobject 1 Answer
Game Object doesn't instantiate at Mouse Position 3 Answers
Transform Position is not moving the target 2 Answers
Assinging new coordinates (position) in Unity to imported assets/objects from Rhino 0 Answers