- Home /
LookAt but using physics AddRelativeTorque
Hello all!
I posted a similar question before but did not get a response that really answered the question. I think its because it was badly worded, so here is another shot :
I am looking for a way to use the rigidbody physics engine to turn an object so that "forward" is facing a target object. I know I can do this easily via the LookAt function, but I want it to be completely physics based. Here is what I have thus far :
function TurnRight(){ if(torqueRight < 20){rigidbody.AddRelativeTorque(Vector3.up * turnPower); torqueRight ++;}} function TurnLeft(){ if(torqueRight >-20){rigidbody.AddRelativeTorque(Vector3.up * -turnPower); torqueRight --;}}
function TurnUp(){ if(torqueUp < 20){ rigidbody.AddRelativeTorque(Vector3.right -turnPower); torqueUp ++;}} function TurnDown(){ if(torqueUp > -20){ rigidbody.AddRelativeTorque(Vector3.right turnPower); torqueUp --;}}
function SpinRight(){ if(spin < 5){rigidbody.AddRelativeTorque(Vector3.forward -turnPower .25); spin ++;}} function SpinLeft(){ if(spin >-5){rigidbody.AddRelativeTorque(Vector3.forward turnPower .25); spin --;}}
function StabalizeUp(){ while(torqueUp > 0){ TurnDown(); } while(torqueUp < 0){ TurnUp(); } } function StabalizeRight(){ while(torqueRight > 0){ TurnLeft(); } while(torqueRight < 0){ TurnRight(); } } function StabalizeSpin(){ while(spin > 0){ SpinLeft(); } while(spin < 0){ SpinRight(); } }
With that code, I am able to turn my object using phyiscs any given way. Up, Down, Left, Right, and spin, are all represented. In addition, I have some functions that stabilize the rotation at any given moment.
Now, what I am looking for, is a way to "aim" using these functions, toward an object. I am currently using this :
function Aim(){ if(aimX > 0.01){ TurnRight(); //SpinRight(); } if(aimX < 0.01){ TurnLeft(); //SpinLeft(); } if(aimX > -0.01 && aimX < 0.01){ StabalizeRight(); //StabalizeSpin(); }
if(aimY > 0.01){
TurnDown();
} if(aimY < 0.01){
TurnUp();
} if(aimY > -0.01 && aimY < 0.01){
StabalizeUp();
}
}
where the aimY and aimX come from :
fwd = transform.forward;
var testAim : ShipTargeting = aimAssist.GetComponent("ShipTargeting"); aim = testAim.fwd;
aimX = fwd.x - aim.x; aimY = fwd.y - aim.y;
and what I am doing is I have a gameobject, aimAssist, attached to the rotating object. aimAssist is using LookAt to aim at the target.
However, my ship is still not lining up properly. Is there a better way to figure out how to add the torque of my rotation functions so that the object looks toward the target?
Answer by hypnoslave · Jan 01, 2011 at 07:01 PM
I don't mean to deconstruct, but what are you trying to accomplish here? why does this need to be all done with physics?
Well, this is all for the AI of space ships for a turret defense style game I am making. Since these ships are going to be moving around using thrust, I want them to turn using thrust. If I don't do this all with physics, the rigidbody velocity measurements get skewed and my turrets lose all accuracy. Plus, it doesn't have that authentic space 3d feel without using physics, otherwise it becomes jerky movement and unrealistic.
I'm not sure how it would be done, but I assure you that it would look exactly the same (and just as smooth) as slerping the rotation to the target, and the slerp would definitely perform better.
What about velocity? If I am changing the rotation with slerp, and adding "forward" thrust using physics, won't my rigidbody velocity measurement be screwed up?