- Home /
CharacterController Slope Problems
Hello, I have a game where my character is moving forwards and when it reaches a slope incline of 15 degrees it slows down because i set the characters motion by setting targetDirection.z =1.0;
But, on slopes the character has trouble, I want to maintain its speed while going up an incline i tried messing with targetDirection.y = 16.0f but when i did that it, the character started to bounce up and down and I couldn't control the jump, so i lowered it to like 1.0f, and i didn't notice anything and the jump was wierd because it keeps me in the air above the ground floating.
I wanted to like rotate the characters axis origin to match the slope of the plane its on, but i dont know how to do that here is my character controller code (its modified)
function UpdateSmoothedMovementDirection ()
{ var lockCameraTimerSaved; var controller : CharacterController = GetComponent(CharacterController); 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 = 1;
if (b_onAndroid)
{
h = -Input.acceleration.y*.5;
}
else
{
//For Desktop uncomment this otherwise PlayMaker FSM sets it.
h = Input.GetAxisRaw("Horizontal");
}
var wasMoving = isMoving;
isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
// Target direction relative to the camera
targetDirection = Vector3.zero;
//move character forward
targetDirection.z = 1.0;
// Grounded controls
// 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)
{
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;
targetSpeed *= runSpeed;
_characterState = CharacterState.Running;
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
}
Also, running downhill its messed up as well
Your answer
Follow this Question
Related Questions
How do I make my character fall off slopes grater than its slope limit? 0 Answers
Sliding down a slope with a character controller 1 Answer
A node in a childnode? 1 Answer
2D Game RigidBody Vs. Character Controller 2 Answers
How to rotate a character controller by exactly 90 degrees with one key 1 Answer