- Home /
Rigidbody Version of RotateAround
I'm making a game and the current script uses transform.RotateAround to move in a circle around another game object. The down side of this is that the game object moving will go through other Trigger colliders, making it useless against another set coding I have. I need some way to manipulate the rigid body by converting Transform.RotateAround.
Coding:
var degree: float = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
transform.RotateAround (Target.position, Vector3.up, -degree);
//Moves towards the target or away depending on up or down key
var translate: float = Input.GetAxis ("Vertical") * speed * Time.deltaTime;
rigidbody.AddForce(0, 0, translate * 50);
Trigger Script:
function OnTriggerEnter(other: Collider){
if(other.gameObject.CompareTag("Wall")){
rigidbody.velocity = -rigidbody.velocity;
}
}
Answer by Statement · Mar 30, 2011 at 02:15 AM
For the trigger: You don't need triggers to bounce stuff off though. Just use a regular collider without any renderer. You can set layermasks so it only affect the ball if you want. Finally you could apply a bouncy physics material to it so it bounces right off.
For the controls: I toyed around a bit with this. See if this is somewhat like you expect.
I created a webplayer so you can try the controls in action.
var speed = 10.0f; var target : Transform;
function FixedUpdate () {
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var direction = (target.position - rigidbody.position).normalized;
var rotation = Quaternion.AngleAxis(horizontal * 60, Vector3.up) * direction * Mathf.Abs(horizontal);
var desired = rotation + vertical * direction;
var change = desired * speed - rigidbody.velocity;
// Debug lines: Red - current heading
// Blue - applied heading
Debug.DrawLine(rigidbody.position, rigidbody.position + rigidbody.velocity, Color.red);
Debug.DrawLine(rigidbody.position, rigidbody.position + change, Color.blue);
rigidbody.AddForce(change * Time.deltaTime, ForceMode.VelocityChange);
}
Answer by efge · Mar 20, 2011 at 07:46 PM
You could use Rigidbody.AddTorque or Rigidbody.AddRelativeTorque.
Edit:
var torque : float = Input.GetAxis("Horizontal");
rigidbody.AddRelativeTorque (0, torque, 0);
(And remember: Put all physics related stuff in the function FixedUpdate.)
I don't know how to convert the data from the Transform.RotateAround to AddTorque.
Edited my answer. (You could also multiply torque and speed.)