- Home /
Allow y rotation
Hello. I am using this code to orient the player to the slope of whatever is below it:
function Update () {
var hit : RaycastHit;
var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);
if (Physics.Raycast (castPos, -transform.up, hit)) {
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
transform.rotation.z = 0;
}
}
This makes the player rotate to the ground. The thing is, it completely restricts their y rotation and you can only move forward/backwards. How do I release the y axis? I only really need the x to rotate up or down, with the z frozen.
The player is controlled with the following in a different script:
var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
}if (!swimming && controller.isGrounded){
controller.SimpleMove(forward * curSpeed);
Thank you
Answer by burnumd · May 20, 2011 at 08:24 PM
In the code you posted, you're manually setting the Transform rotation every frame, which may override any Input you're getting.
So one solution is to cache the y-offset of how much your player should be rotated and, whenever you're aligning the player, apply that offset imediately.
private var yRotationOffset : float = 0;
function Update () {
var hit : RaycastHit;
var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);
if (Physics.Raycast (castPos, -transform.up, hit)) {
transform.up = hit.normal; //This is a shorter way of aligning the player to the surface's normal.
transform.Rotate (0, yRotationOffset, 0); //Apply our cached y-rotation offset.
}
else
{
transform.Rotate (0, Input.GetAxis ("Horizontal"), 0); // If we're not aligning the player, we can apply the rotation directly
}
yRotationOffset += Input.GetAxis ("Horizontal");
}
If you're modifying the player's rotation in some other script's update, there's no way to determine which will happen first. What's likely happening is that you're taking the player input and doing that rotation and then this script comes along and overwrites it. The simplest solution is to move your raycast code into the same script, above where you take input and do rotation based on that. Other options you might try include notifying your input script when the player's orientation has been determined and only then taking input that frame.
Thanks for replying! It still restricts y rotation though. It's not for mouselook, just for a character.
Without knowing what you're doing to rotate the character, it's hard to say what's preventing it. I should have been clearer that setting transform.up is still potentially going to override input, depending on how it's being handled.
Right, but it's relevant what's happening in that other script. We know that in this script, the transform is being rotated every frame to match the slope of whatever's below it. How does that other script rotate the player? Is it done in an absolute fasion (as with the included $$anonymous$$ouseLook script), or a relative fashion? If it's absolute, you're going to get a conflict with what you're trying to do here.
methinks that it's absolute, but I need them to work together. I included the script below--