Question by
bennettfam93 · Oct 23, 2020 at 09:48 PM ·
c#charactercharactercontroller
Character Controller not going up terrain hill
https://youtu.be/H9rUKtPgkm4 Why is this happening? Here is the code: using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class SC_1PController : MonoBehaviour
{
public float walkingSpeed = 7.5f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
Terrain terrain;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
void Start()
{
terrain = Terrain.activeTerrain;
characterController = GetComponent<CharacterController>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftBracket))
{
lookSpeed -= 1;
}
if (Input.GetKeyDown(KeyCode.RightBracket))
{
lookSpeed += 1;
}
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
if (characterController.transform.position.y - 0.69f >= terrain.SampleHeight(transform.position))
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
Comment
Best Answer
Answer by rhapen · Oct 24, 2020 at 10:12 AM
Try increasing following parameters on Character Controller Component Skin Width to 0.1 and Step Offset to at least 0.1 or more
Your answer
Follow this Question
Related Questions
MoveTowards is curving for no reason 0 Answers
How to crouch a FPS Controller? 2 Answers
Setup an animation script, but now mouselook is not working? 0 Answers
Character Rotation 0 Answers
I want to add some Velocity controll to my CharacterController 1 Answer