- Home /
How do I align my player transform to waypoints and without restricting Z Axis rotation?
In the diagram above as my selected player character moves along the U shaped path I am trying to align to the direction of the waypoints(grey balls) and along with this i also want to unrestricted the Z axis rotation. Currently i am doing this
thisTrans.forward = currWp.thisTransform.forward;
With this i am able to align my character to the waypoints but the Z axis rotation is locked resulting in the following
I am looking for a solution where my character aligns to the waypoints and slides smoothly over the U Shaped track
Answer by unity_ek98vnTRplGj8Q · Aug 25, 2020 at 09:23 PM
Assuming you are using collision to keep the player on the track, you could grab the up vector of the point you are contacting on the ground (the ground's normal vector) and us it as the up vector of your player via Quaternion.LookRotation()
private Vector3 groundNormal;
public LayerMask groundLayer;
void Start(){
groundNormal = Vector3.Up;
}
void Update(){
//Use LookRotation with a custom up vector
transform.rotation = Quaternion.LookRotation(currWp.thisTransform.forward, groundNormal);
}
void OnCollisionStay(Collision col){
//If we are colliding with the ground
if(col.gameObject.layer == groundLayer){
//Get the "Up" direction of the ground at the point you are contacting. This is what we want our player's "Up" direction to be
groundNormal = col.contacts[0].normal;
}
}
Answer by stark89 · Aug 25, 2020 at 09:14 PM
At 3:00 am i think i found something that is remotely close
Vector3 eulerAngle = currWp.thisTransform.rotation.eulerAngles;
Vector3 transEuler = thisTrans.rotation.eulerAngles;
thisTrans.rotation = Quaternion.Euler(transEuler.x, eulerAngle.y, transEuler.z);
Is there a cleaner, more neat way to do this?
Thanks in advance