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?