Quantcast
Channel: Questions in topic: "issue"
Viewing all articles
Browse latest Browse all 827

Character is bouncing infinitely after I implimented gravity

$
0
0
I am new to Unity. I used a tutorial from Single Sapling Games on youtube on how to make character movement. Everything was working until just after I implimented the gravity code, at which point the character began to bounce infinitely on the ground. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { //VARIABLES [SerializeField] private float moveSpeed; [SerializeField] private float walkSpeed; [SerializeField] private float runSpeed; private Vector3 moveDirection; private Vector3 velocity; //Gravity [SerializeField] private bool isGrounded; [SerializeField] private float groundCheckDistance; [SerializeField] private LayerMask groundMask; [SerializeField] private float gravity; //REFERENCES private CharacterController controller; private void Start() { controller = GetComponent(); } private void Update() { Move(); } private void Move() { isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask); if(isGrounded && velocity.y <0) { velocity.y = 2f; } float moveZ = Input.GetAxis("Vertical"); moveDirection = new Vector3(0, 0, moveZ); if (isGrounded) { if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) { //walk Walk(); } else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift)) { //RUN Run(); } else if (moveDirection == Vector3.zero) { //IDLE Idle(); } moveDirection *= moveSpeed; } controller.Move(moveDirection * Time.deltaTime); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } private void Idle() { } private void Walk() { moveSpeed = walkSpeed; } private void Run() { moveSpeed = runSpeed; } }

Viewing all articles
Browse latest Browse all 827

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>