i’m quite new to coding and unity but iv’e had great fun following some tutorials, i seem to have run into a problem that is mainly just a nuisance, when i first played around with character movement, i used the rigid body method using AddForce, however this time around i have used a character controller and i am using the Transfrom.Position method now, the issue I’m having is i can jump and land on a cube (for reference) but if i jump and hold the forward key while against the cube, my character then jitters and vibrates, i have gravity and check IsGrounded active. my code is as follows.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
float speed = 7f;
float run = 11f; /// sprint speeed
float sneak = 5f; // crouch speed
public float gravity = -9.81f;
public Object PlayerBody;
public Transform groundCheck;
public float GroundDistance = 0.4f;
public LayerMask groundmask;
Vector3 velocity;
bool isGrounded;
public float JumpHeight = 3f;
public KeyCode sprint;
public KeyCode Jump;
// Update is called once per frame
void Update()
{
//check if the player is grounded
isGrounded = Physics.CheckSphere(groundCheck.position, GroundDistance, groundmask);
if (isGrounded && velocity.y <0)
{
velocity.y = -2f;
}
float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (isGrounded && Input.GetKey(Jump)) //<<<< checks what key is being pressed at the current time, in this instance we are looking to see if Jump is being pressed
{
velocity.y = Mathf.Sqrt(JumpHeight * -2f * gravity);
}
if (Input.GetKey(sprint)) // <<<<<<< this is how to get a keybind key input, using “public KeyCode”
{
controller.Move(move * run * Time.deltaTime);
} else {
controller.Move(move * speed * Time.deltaTime);
}
velocity.y += gravity * Time.deltaTime; // remember += means pluss itself, so velocity equals |velocity| pluss gravity times by Time.DeltaTime
controller.Move(velocity * Time.deltaTime);
please help.