- Home /
Keep flies in a sphere?
Hello everyone.
I have a sphere. In this sphere, I instantiate random cubes (flies). And the flies are flying back and forth (trembling). Now I want, that all the flies are staying in this sphere. Leaving not the sphere. How can I do that. Please look Fig. 1:
In this example, I want that the flies fly from one sphere to another sphere. Please look Fig. 2:
How can I solve this?
What must I search? What are the search words. Has somebody some some ideas. How can I solve that?
Oh, hey, did you know that your name backwards is jdracula? Is that supposed to mean something? Raises eyebrow
Answer by jpthek9 · Jan 14, 2015 at 02:37 AM
Vector3 LastPosition
public Vector3 SphereCenter;
public float SphereRadius;
void FixedUpdate()
{
if (Vector3.Distance(transform.position, SphereCenter) > SphereRadius)
{
rigidbody.position = lastPosition;
}
LastPosition = transform.position;
}
This will keep your flies inside your sphere at all times. To move your flies to another sphere, just change SphereCenter. If you want to make the sphere bigger, change SphereRadius.
Your answer
Follow this Question
Related Questions
Networked position interpolation implementation (not Dead Reckoning)? 1 Answer
Help!How to validate the client position in headless unity? 0 Answers
how to make an object move like the one it is in contact(moving using Animator) with in unity? 1 Answer
Ignoring the transform.y position on movement 2 Answers
Problem with a motion script 0 Answers