- Home /
Sphere movement very erratic and uncontrollable.
Okay, I feel like an idiot asking so many questions. But if you have seen my questions before I was asking how to get a sphere to rotate physically, and how to get a camera to follow it. Both of those were done Yay! But i'm currently faced with a problem. When it starts to rotate, and move. It doesn't seem to stop. Go ahead and open the scripts I post and try it to see what I mean. I have no idea why it's doing it. I thought making it relative would work but I guess not. Here is my code
Sphere Movement:
function FixedUpdate () {
if (Input.GetButton("W"))
{
rigidbody.AddRelativeTorque (0, 0, 2);
rigidbody.AddRelativeForce (0, 0, 2);
}
if (Input.GetButton("S"))
{
rigidbody.AddRelativeTorque (0, 0, -2);
rigidbody.AddRelativeForce (0, 0, -2);
}
if (Input.GetButton("A"))
{
rigidbody.AddRelativeTorque (-2, 0, 0);
rigidbody.AddRelativeForce (-2, 0, 0);
}
if (Input.GetButton("D"))
{
rigidbody.AddRelativeTorque (2, 0, 0);
rigidbody.AddRelativeForce (2, 0, 0);
}
}
Camera:
var target: Transform;
var damp: float = 0.2;
var distance: float = 50;
function Update(){
damp = Mathf.Clamp(damp, 0.01, 10); // clamps damping factor
var pCam = transform.position;
var pTarget = target.position;
var diff: Vector3 = pTarget - pCam; // diff = difference between positions
var dist = diff.magnitude; // dist = distance between them
if (Mathf.Abs(diff.y) < 0.7*distance){
diff.y = 0; // doesn't modify camera height unless angle > 45
}
if (dist>distance){ // if distance too big...
diff *= 1-distance/dist; // diff = position error
// move a FPS independent little step towards the ideal position
transform.position = pCam + diff * Time.deltaTime/damp;
}
transform.LookAt(pTarget);
}
Please, to really understand what I mean. Make a blank sphere on a map with a camera and apply these scripts.
Does the sphere's rigidbody have a reasonable angular drag?
Torque is an "angular force" that causes both displacement and rotation. You need an angular drag to have the angular velocity stop over time, and a linear drag to have the linear velocity stop over time. Your script works as intended on my comp when the drag and angular drag are set up to a nice value.
Do you $$anonymous$$d commenting in the values you used?
Answer by Waz · Jul 07, 2011 at 06:56 AM
Your code is torquing the object around the axis of motion. That seems wrong: to roll forward (0,0,1), you rotate around the X axis (1,0,0). Run your code with the AddRelativeTorque functions commented-out and then with the AddRelativeForce functions commented out, and see that both are what you intended.
I just tried both of the things you two suggested. I tried editing the script so now it's:
function FixedUpdate () { if (Input.GetButton("W")) {
rigidbody.AddRelativeTorque (2, 0, 0);
//rigidbody.AddRelativeForce (2, 0, 0); }
if (Input.GetButton("S")) {
rigidbody.AddRelativeTorque (-2, 0, 0); //rigidbody.AddRelativeForce (-2, 0, 0); }
if (Input.GetButton("A")) { rigidbody.AddRelativeTorque (0, 0, -2); //rigidbody.AddRelativeForce (0, 0, -2); }
if (Input.GetButton("D")) { rigidbody.AddRelativeTorque (0, 0, 2); //rigidbody.AddRelativeForce (0, 0, 2); } }
But it still gets into an uncontrollable spin.