I have a character controller on my main character on a third-person controller game. I managed to control the character while moving and jumping but a colliding bug remains.
The problem is that when my character collides with other colliding objects like terrain collider or capsule collider, he immediately rotates to 90 or -90 degrees on Y axis. How can I solve this so my character gets some smooth collision and not this irritating auto-rotation?
I’ve been testing until now and as far as I can see the problem is FaceMovementDirection() that is causing this.
I simplified my script for faster approach:
#pragma strict
// This script must be attached to a GameObject that has a CharacterController
@script RequireComponent( CharacterController )
var moveJoystick : EasyMobileJoystick;
var rotateJoystick : EasyMobileJoystick;
var cameraPivot : Transform; // The transform used for camera rotation
var cameraTransform : Transform; // The actual transform of the camera
var speed : float = 5; // Ground speed
var jumpSpeed : float = 8;
var inAirMultiplier : float = 0.25; // Limiter for ground speed while jumping
var rotationSpeed : Vector2 = Vector2( 50, 25 ); // Camera rotation speed for each axis
private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3; // Used for continuing momentum while in air
private var canJump = true;
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
// Move the character to the correct start position in the level, if one exists
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function FaceMovementDirection()
{
var horizontalVelocity : Vector3 = character.velocity;
horizontalVelocity.y = 0; // Ignore vertical movement
// If moving significantly in a new direction, point that character in that direction
if ( horizontalVelocity.magnitude > 0.1 )
thisTransform.forward = horizontalVelocity.normalized;
}
function OnEndGame()
{
// Disable joystick when the game ends
moveJoystick.Disable();
rotateJoystick.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update()
{
var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
// We only want the camera-space horizontal direction
movement.y = 0;
movement.Normalize(); // Adjust magnitude after ignoring vertical movement
// Let's use the largest component of the joystick position for the speed.
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
// Check for jump
if ( character.isGrounded )
{
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
// Adjust additional movement while in-air
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
if ( character.isGrounded )
// Remove any persistent velocity after landing
velocity = Vector3.zero;
// Face the character to match with where she is moving
FaceMovementDirection();
// Scale joystick input with rotation speed
var camRotation = rotateJoystick.position;
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
// Rotate around the character horizontally in world, but use local space
// for vertical rotation
cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
cameraPivot.Rotate( camRotation.y, 0, 0 );
}
Oh well year, if you are always facing the direction you are moving and then moving the direction you are facing… You will make turns. When you are colliding with a diagonal surface and you are containing to try to move forward even just one frame, you will move slightly sideways right? so if you are moving slightly sideways you will rotate into that direction if you are using your FaceMoveDirection(); function.
If you do not want to alter your script too much, change this variable to a higher number and see. Otherwise you might want to consider a different approach.