- Home /
Can't get gravity to work on Character Controller
Hello I was hoping somebody could help. I'm following a tutorial and I cant get the gravity to work in a 3d FPS environment. The player either flies into sky, creeps across the floor and up the walls or if set to 0 The player can essentially fly. My editor isn't throwing any errors and the script is working in conjunction with a Character Controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(CharacterController))]
[AddComponentMenu ("Control Script/FPS Input")]
public class FPSInput : MonoBehaviour {
private CharacterController _charController;
void Start() {
_charController = GetComponent<CharacterController> ();
}
//Var for player / speed
public float speed = 6.0f;
// var for gravity intensity
public float gravity = -9.8f;
// character movement
// Update is called once per frame
void Update () {
//transform.Translate (0, speed, 0);
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
//transform.Translate (deltaX * Time.deltaTime, 0, deltaZ * Time.deltaTime);
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude (movement, speed);
movement.y = gravity;
movement *= Time.deltaTime;
movement = transform.transform.TransformDirection (movement);
_charController.Move (movement);
}
}
did you try to write 2 in the Gravity field from your FPS Input Script just try a few numbers maybe that could help
Hello $$anonymous$$e, I have indeed although I'm testing it again for good measure. Gravity 0.33 = slowly floating into space Gravity -1.39 = Slow gliding across the ground
No luck with either of these.
Well im sry that i have to say i dont know how to fix that for me all look good
No worries $$anonymous$$e1999, tinkering with it I have a feeling it something to do with the capsule collider that comes Controller. Not so much the code. As you tilt the camera forward that's when the player slides around. If you try to get the player to stand upright, it stays still. very weird. Thanks,
if you want to have a fps controller why you did not use the FirstPersonController prefab from the Standard Assets ? i took that and just modified two or three things and had an vr touchpad controller its easy to use :) maybe you should try that
Answer by thedetective456 · Oct 21, 2020 at 09:58 AM
Hi for someone still wondering to do it in the simples way :
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
private CharacterController cc;
private void Start()
{
cc = GetConponent<CharacterController>();
}
void Update()
{
cc.Move(Physics.gravity * Time.deltaTime);
}
}
Answer by Yuvii · Oct 09, 2017 at 12:25 PM
Hey,
well i guess you already checked, but here's the script in the unity documentiation
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
I can see there's some difference between this one and yours. Try to get closer from this script it may solve the problem. I'm not familiar with the unity's character controller, i'm used to make my own scripts, but i hope this can help.
Hello, Thank you Yuvii. I'm aware of documentation although i'm trying to follow a tutorial from Unity in Action. It has me using the character controller. If I deviate too much from what it is telling me what to do it could ruin the following chapters. Although I will bank you answer for a later date.
Your answer
Follow this Question
Related Questions
How to move while jumping? 1 Answer
Lunging with Character Controller 0 Answers
Player Control. Roller Ball with Cube Acceleration. 1 Answer
Unity AI character controller gravity 0 Answers
3D Hook With Swing Physics 0 Answers