I am following a Brackeys tutorial on YouTube. Things were running smoothly until we fixed the ever-increasing velocity. The velocity was still ever-increasing, and fixing it, I made it worse. Now there is no gravity and i’m outside of the characters body. Help!
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask; //all of the public variables are assigned
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
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);
}
}
With character controller you have to apply gravity yourself, if you are using Move, and not using SimpleMove to move it (which applies gravity automatically).
Your code doesn’t look the same as in video, you are missing a line.
I’d recommend also formatting the code, so it looks properly formatted, and in this case, if you are following a tutorial, format it like in the tutorial until you get a little bit more experience. That way you can also visually detect very easily what is missing.
Hi, I am having a similar issue and I cannot seem to figure out why it’s happening. My character is flying! He won’t take the stairs. Have I properly enabled gravity to work? I am using move to control it. Here’s my script, I’ve even created an isGrounded empty to feed the log and tell me if my controller is actually making contact with the floor, and it keeps returning false! What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 15;
private Vector3 move;
public float gravity = -10;
public float jumpForce = 8;
private Vector3 velocity;
public Transform groundCheck;
public LayerMask groundLayer;
private bool isGrounded;
public FixedJoystick joystick;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// float x = Input.GetAxis("Horizontal");
// float z = Input.GetAxis("Vertical");
float x = joystick.Horizontal;
float z = joystick.Vertical;
move = transform.right * x + transform.forward *z;
controller.Move(move * speed * Time.deltaTime);
isGrounded = Physics.CheckSphere(groundCheck.position, 1.3f, groundLayer);
Debug.Log(isGrounded);
}
}
Your script doesn’t contain any code to apply gravity to the Vector3move.
Here’s a basic movement script that also applies gravity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 10;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
private float verticalVelocity;
private float gravityValue = 9.81f;
Vector3 moveDir = Vector3.zero;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
bool groundedPlayer = controller.isGrounded;
// slam into the ground
if (groundedPlayer && verticalVelocity < 0)
{
// hit ground
verticalVelocity = 0f;
}
verticalVelocity -= gravityValue * Time.deltaTime;
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.sqrMagnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
}
else
{
moveDir.x = 0f;
moveDir.z = 0f;
}
moveDir.y = verticalVelocity;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
It’s a basic Third-Person controller script that applies gravity to controller (like you wanted,) but also does more than that, such as move the direction the camera is facing, and also turn the controller to face that direction as well.