- Home /
Keeping transform.forward facing forwards
Hi guys,
I've been wrestling with a problem involving a physics movement script. The script is great, but I recently added in being able to look up and down using torque on the rigidbody.
This is also great, though what it means is that the transform.forward is rotated in the x plane (look up/down), which means when I apply a force in that direction to make the player move forward, it does a loop the loop as it tries to follow the player's transform.forward.
I thought I could negate this by applying the opposite Quaternion rotation as shown in my code, but this has seemingly no effect on the loop-the-loop behaviour.
Can anyone help me? (Excuse the code formatting, I extracted all that was relevant I hope)
Cheers,
Popuppirate
void Update(){
Quaternion rotation = new Quaternion(-transform.rotation.x, 0f, 0f, -1);
true_forwards = rotation * transform.forward;
currVertThrust = 0.0f;
float aclVertAxis = Input.GetAxis ("Vertical");
if (aclVertAxis > deadzone) {
currVertThrust = aclVertAxis * forwardAcl;
} else if (aclVertAxis < -deadzone) {
currVertThrust = aclVertAxis * backwardAcl;
}
if (somebool) {
float tiltAxis = Input.GetAxis ("Mouse Y");
currTilt=Mathf.Clamp (currTilt,-10f, 10f);
if (Mathf.Abs (tiltAxis) > deadzone) {
currTilt += tiltAxis;
} else {
currTilt=0.0f;
}
}
void FixedUpdate(){
RaycastHit hit;
for (int i=0; i<hoverPoints.Length; i++) {
var hoverPoint=hoverPoints[i];
if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hit, hoverHeight, layerMask)){
body.AddForceAtPosition(Vector3.up*hoverForce*(1.0f-(hit.distance/hoverHeight)), hoverPoint.transform.position);
} else if(someotherbool) {
if (transform.position.y>hoverPoint.transform.position.y){
body.AddForceAtPosition(hoverPoint.transform.up*hoverForce,hoverPoint.transform.position);
} else {
body.AddForceAtPosition(hoverPoint.transform.up*-hoverForce,hoverPoint.transform.position);
}
}
}
if (Mathf.Abs (currVertThrust) > 0) {
body.AddForce(true_forwards*currVertThrust);
}
if (Mathf.Abs (currHorizThrust) > 0) {
body.AddForce(transform.right*currHorizThrust);
}
if (someotherbool) {
if (currTurn > 0) {
body.AddRelativeTorque (Vector3.up * currTurn * turnStrength);
} else if (currTurn < 0) {
body.AddRelativeTorque (Vector3.up * currTurn * turnStrength);
}
if (currTilt>0){
body.AddTorque (Vector3.Cross (transform.forward, transform.up)*currTilt*turnStrength*0.2f);
}
else if (currTilt<0){
body.AddTorque (Vector3.Cross (transform.forward, transform.up)*currTilt*turnStrength*0.2f);
}
}
}
}
Answer by DMGregory · Nov 19, 2014 at 08:18 PM
If you just want the "horizontal forward" (ie. the forward direction projected into the world's horizontal/xz plane) you can get it like this:
Vector3 horizontalForward = transform.forward;
horizontalForward.y = 0f;
horizontalForward.Normalize();
Or if you want to be able to get any local direction into this horizontal plane, you can do this:
Quaternion horizontalOrientation = Quaternion.LookRotation(Vector3.up, -transform.forward) * Quaternion.AngleAxis(90f, Vector3.right);
Vector3 arbitraryVector = horizontalOrientation * new Vector3(rightwardAmount, 0f, forwardAmount);
Thanks D$$anonymous$$G,
I tried both methods and my object, while not flipping over, still gains upwards momentum. I want purely lateral momentum. Any thoughts as to why this might be?
For whatever reason my script was not updating- now it is and it works great! Cheers!