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*;*
  •  if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hit, hoverHeight, layerMask)){*
    

_ body.AddForceAtPosition(Vector3.uphoverForce(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)currTiltturnStrength*0.2f);_

  •  			}*
    
  •  			else if (currTilt<0){*
    

_ body.AddTorque (Vector3.Cross (transform.forward, transform.up)currTiltturnStrength*0.2f);_

  •  			}*
    
  •  		}*
    

}

  •  }*
    

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);