- Home /
Character Rotation
function UpdateSmoothedMovementDirection ()
{
var cameraTransform = Camera.main.transform;
var grounded = IsGrounded();
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
if (v < -0.2)
movingBack = true;
else
movingBack = false;
var wasMoving = isMoving;
isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
// Target direction relative to the camera
var targetDirection = h * right + v * forward;
if (grounded)
{
// Lock camera for short period when transitioning moving & standing still
lockCameraTimer += Time.deltaTime;
if (isMoving != wasMoving)
lockCameraTimer = 0.0;
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
if (targetDirection != Vector3.zero)
{
// If we are really slow, just snap to the target direction
if (moveSpeed < walkSpeed * 0.9 && grounded)
{
moveDirection = targetDirection.normalized;
}
// Otherwise smoothly turn towards it
else
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
// Smooth the speed based on the current target direction
var curSmooth = speedSmoothing * Time.deltaTime;
// Choose target speed
//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
_characterState = CharacterState.Idle;
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
// Reset walk time start when we slow down
if (moveSpeed < walkSpeed * 0.3)
walkTimeStart = Time.time;
}
else
{
if (jumping)
lockCameraTimer = 0.0;
if (isMoving)
inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
}
}
Please format your code better. This is truly disgusting.
Answer by fafase · Dec 14, 2011 at 08:29 AM
Hey, I guess you want your guys to move forward and backward with W and S and simply rotate but no displacement with A and D. This simple code should do the trick, they are supposed to be used with the arrow pad though:
var speed = 3.0; //Speed you guy will move forward/backward var rotateSpeed = 3.0; // speed at which it will rotate around Y-axis
function Update () { var controller : CharacterController = GetComponent(CharacterController); // this get you character controller attached to the object
//rotate around y-axis
transform.Rotate(0, Input.GetAxis ("Horizontal")* rotateSpeed, 0);
//Move Forward-Backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
}
@script RequireComponent(CharacterController)
Let us know if that worked for you. I might have understood your question wrong.
Oh yes it words fine thankyou ^_^
However, is there a method to rotate it just 90 degrees once ins$$anonymous$$d of continuing to rotate?
well you can give it 90deg rotation on the y axis but pb would be that your guy would turn 90deg each frame so you would not even see it spinning so you might have to change the input checking and use a input.getkeydown. Have a look http://unity3d.com/support/documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html and http://unity3d.com/support/documentation/ScriptReference/Transform.Rotate.html
$$anonymous$$y advice, take the code that is working then apply the new input function, look at the unity explanations, and for the transform.rotate, start to alter the parameters until you get what you want. You might even find better than you were expecting and you will learn the effect of each parameters. $$anonymous$$y hint, you need quaternion which is something quite complicated to master.
Thanks a lot to you ^_^
I guess i will have more trouble with quaternion too.
Nice day (:
Your answer
Follow this Question
Related Questions
Activating things- What on earth am I doing wrong??? 2 Answers
Character Rotation 2 Answers
C# CharacterMotor crashing. What am I doing wrong? 1 Answer
Velocity for movement 0 Answers
Character-Only Shadows 1 Answer