- Home /
Circular movement of gameobject with rigidbody
Hello, everybody! I'm trying to make a game, and I need gameobjects with rigidbody to move circular around mouse position in some radius, when player clicks mouse button (like planet's gravity simulation). I've found some decision, which use transform.position to do that (It's not full):
if (Input.GetMouseButton (1)) {
timer += Time.deltaTime;
transform.position = new Vector3 (2*Mathf.Cos(Mathf.Deg2Rad*timer), 2*Mathf.Sin(Mathf.Deg2Rad*timer), 0);
}
But i need gameobjects to save them kinetic energy, that's why i want to use rigidbody. Also, gameobjects which have moved before player clicks button, should also save them kinetic energy, so I want to use AddForce method. Can anyone help me in writing this code? Thank you!
Answer by toddisarockstar · Feb 20, 2015 at 07:26 AM
its much simplier than you think. anytime you want something to spin a circle, you make it a "child" of the object that is where you want the center of the rotate point. then spin the parent object!
code might look like this:
var spinpoint:GameObject;
var spinme=false;
function Update(){
if (Input.GetMouseButton (1)) {transform.parent=spinpoint.transform;spinme=!spinme;}
if(spinme){
spinpoint.rotation.y=spinpoint.rotation.y+0.01;}
}
make an emptyobject and drop it into the inspector to assign what "spinpoint" is.