- Home /
Character Controller strange collision bug
Hey,
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?
Thanks in advance.
this is not a bug within unity. There is something along the way of your setup that would be causing this issue. What method are you using to control the character, post code. Are there any other colliders in your character besides the charactercontroller's capsule collider.
I am using the Camera Relative Control script (I modified it but the core is the same) which uses character.$$anonymous$$ove() method to move based on joystick direction and speed. I have a box collider on the weapon but I don't think that it is causing this problem because I disabled the hole weapon and it is still happening. Can "Generate Colliders" on FBX Import Settings trigger this issue? I think I let it unchecked when I added the object on scene..
Thanks for the answer.
No, if you left it unchecked then it would not use colliders from the FBX. Go into the example projects and and load the Camera Relative Control scene with the original script. Place a box in the scene and ad a texture to the character so you can tell if he is rotating, then run the default character from that scene into the box. If it does not happen, add your character ins$$anonymous$$d, if it still does not happe, add your control script ins$$anonymous$$d of the default one, if it happens post your code.
Answer by Anxo · Jan 27, 2013 at 11:01 PM
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.
if ( horizontalVelocity.magnitude > 0.1 )
Thanks a lot for clearing this up for me. No, changing the variable doesn't work for my case so what would you suggest as the best approach?
Well I am not sure what your game is about but if you need it to face direction only when you are telling it to change direction then you can add a bool that checks if the left right arrow are pressed before it applies face direction. It would be very relative to your project.
I don't think it will solve the problem in my game. The issue is that my character is going to do some melee combat which means that it will collide with enemies while running, changing direction and attacking. How can I synchronize the characters direction with left joystick position so I change characters direction only when the joystick has changed the position? How would I calculate this direction?
I think this will solve the problem because I won't change the direction of my character just because the character controller is going to move on a different direction (because of the collision)..
You are getting the X and Y intake of the left Joystick right? You can take the X value and say
if(xvalue > number || xvalue < -number){
{
facedirection code
}
That way it will only rotate the character when the joystick is moved left or right.
Yes, I have moveJoystick.position.x and moveJoystick.position.y but how do I calculate the direction that the character needs to be. Lets say that moveJoystick.position.x is -0.5 (left), and I have thisTransform.forward which represents the forward direction of the character. How do I calculate the left direction of the character based on moveJoystick.position.x?
Answer by bardhlohaj · Jan 27, 2013 at 10:54 PM
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 );
}