#Movement issue - Using Character Controller
19 messages · Page 1 of 1 (latest)
Use codeblocks to send code in a message!
To make a codeblock, surround your code with ``` (3 backticks. Click here to see where the key is)
To use syntax highlighting, add the file extension of the language you wish to highlight (cs for C#, cpp for C++)
For example:
```cs
Console.WriteLine("Hello World");
```
Produces:
Console.WriteLine("Hello World");
To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float WalkSpeed = 5f;
public float SprintMultiplier = 2f;
public float JumpForce = 5f;
public float GroundCheckDistance = 1.5f;
public float LookSensitivityX = 1f;
public float LookSensitivityY = 1f;
public float MinYLookAngle = -90f;
public float MaxYLookAngle = 90f;
public Transform PlayerCamera;
public float Gravity = -9.8f;
private Vector3 velocity;
private float verticalRotation = 0f;
private CharacterController characterController;
void Awake(){
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
void Update(){
float horizontalMovement = Input.GetAxis("Horizontal");
float verticalMovement = Input.GetAxis("Vertical");
Vector3 moveDirection = transform.forward * verticalMovement + transform.right * horizontalMovement;
moveDirection.Normalize();
float speed = WalkSpeed;
if(Input.GetAxis("Sprint") > 0){ speed = SprintMultiplier;
}
characterController.Move(moveDirection speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && IsGrounded()){
velocity.y = JumpForce;
}
else{
velocity.y += Gravity * Time.deltaTime;
}
characterController.Move(velocity * Time.deltaTime);
if(PlayerCamera !=null){
float mouseX = Input.GetAxis("Mouse X") * LookSensitivityX;
float mouseY = Input.GetAxis("Mouse Y") * LookSensitivityY;
verticalRotation -= mouseY;
verticalRotation = Mathf.Clamp(verticalRotation, MinYLookAngle, MaxYLookAngle);
PlayerCamera.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
}
bool IsGrounded(){
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.down, out hit, GroundCheckDistance)){
return true;
}
return false;
}
}```
code ^^
no rigidbody?
nope- like I said im really new so i only really follow tutorials and try to learn as I go
just added a rigidbody and started messing around with it, still doesnt fix the issue
don't try random things
debug log your horizontal movement axis for example
see if that number stays above zero after you release the key
for input axes, the input usually doesn't hit zero immediately
if you want it to zero out when you don't press anything, you can enable Snap under your input axis in the input manager
as a follow up to this, I would recommend using Input.GetKeyDown() for the buttons to make it more reactive, atleast over the snapping input just to similfy things or make it easier to understand as someone newer to it
I'll answer this cause to be fair the solution was a little hard to find. The issue can be found when debugging either the horizontal or vertical float variables. You can see that instead of setting between 0 and 1 it is climbing and falling, hitting every number until it gets to either 1 or 0. As a result your not stopping immediately because neither axis variables are hitting zero immediately. This isn't something i've encountered so I assume it has something to do with how the CharacterController component handles inputs. Either way to resolve this you can go to:
Edit -> Project Settings -> Input Manager
open both Vertical and Horizontal and change the "Gravity" value to something higher. The higher this number is the fast the values will fall thus making so stop more abruptly.
I would not recommend this because it requires manually via code associating two keys as opposing inputs for the same axis
for single input things that dont require neddin to read in both axis directions like sprinting and jumpting, shooting, ect its fine
right, I never said not to use single buttons for those stuff
Thank you all so much! The issue is fixed, I just have one more question- looking at the code, do you guys have an idea on how I can make a,s, and d(movement keys) make you go slower than going forward(w)?