- Home /
Controlling an asteroid using a rigidbody.
Hey guys i was wondering if you could give me some advice on how to approach this problem i am having. I have some code written , but its not working the way i want it to. I want the player to control an asteroid that they can move around in space. This is the code i have so far which doesnt work.
public var cameraa:Transform;
var smooth:float =5.0f;
var rotationSpeed:float = 10f;
var accnSpeed:float = 10f;
function Update () {
var currentRotationX:float = gameObject.transform.rotation.x;
if(Input.GetKey(KeyCode.W)) {
if(currentRotationX < 60 || currentRotationX > 360-60 )
gameObject.rigidbody.AddTorque(-rotationSpeed*Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.A)) {
gameObject.rigidbody.AddTorque(0,-rotationSpeed*Time.deltaTime,0);
}
if(Input.GetKey(KeyCode.D)) {
gameObject.rigidbody.AddTorque(0,rotationSpeed*Time.deltaTime,0);
}
if(Input.GetKey(KeyCode.S)) {
if(currentRotationX < 60 || currentRotationX > 360-60)
gameObject.rigidbody.AddTorque(rotationSpeed*Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.X)) {
gameObject.rigidbody.AddRelativeForce(Vector3.forward*accnSpeed*Time.deltaTime);
}
}
At the moment the code makes me rotate too far as i hold down WASD, i want to limit the rotation of the asteroid so it doesnt rotate out of control, i also want it to accelerate in the forward direction of my controller direction. Please help help help , been trying to sort this out for weeks, am not getting far at all... :( im only starting out using unity and cant find any 3rd person controllers for space games that are free. Thanks guys, would appreciate the helo tons
Answer by adrenak · Feb 22, 2012 at 06:27 PM
If you want to prevent the asteroid from spinning out of control then you have to limit the angular velocity to a maximum value use this : rigidbody.maxAngularVelocity = thevalueyouwant;
I an not sure if you should have it in the update function to call it every frame or not. Try making a function Start ( start with a capital s ) and have it in there, if it doesn't work there then try it adding to the update function.
I Hope this helped!
Your answer
Follow this Question
Related Questions
Issue with Rotation behaviour of PID Controller when rotating to look at mouse position 0 Answers
How to Convert dir = (a.transform.position - b.transform.position).normalized; to Degrees ? 1 Answer
Resetting transform's original position and rotation after using transform.rotateAround 1 Answer
click to move workig script!! but pls help with rotation!! :( 1 Answer
Sphere rotation in moving direction 1 Answer