Hello guys. So, I started a mini project and I am having trouble with the physics. Currently, I can jump, then accelerate back down. But, If I say, walk off the edge of an object above the ground, I immediately go to the ground with no acceleration at all. I followed along with this script using a tutorial, so I don’t know enough about it to alter much. Would someone mind explaining this to me? Is there a better way at doing a first person controller? How can I fix this gravity issue??
Heres the script I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewFPC : MonoBehaviour
{
public float movementSpeed = 5f;
public float jumpHeight = 7f;
public float gravityPull = -9.8f;
public float mouseSensitivity = 3f;
private float verticalRotation = 0f;
public Camera fpsCam;
public CharacterController CharController;
// Current vertVel
float verticalVel = 0f;
void Start()
{
fpsCam = GetComponentInChildren<Camera> ();
CharController = GetComponent<CharacterController> ();
}
void Update ()
{
Movement ();
LookRotation ();
}
void Movement ()
{
float forwardSpeed = Input.GetAxis ("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * movementSpeed;
verticalVel += Physics.gravity.y * gravityPull * Time.deltaTime;
// Gravity
Vector3 speed = new Vector3 (sideSpeed, verticalVel, forwardSpeed);
if (CharController.isGrounded && Input.GetButtonDown ("Jump"))
{
verticalVel = jumpHeight;
}
//Rotate speed to match actual rotation
speed = transform.rotation * speed;
// How many units are you travelling by this
CharController.Move (speed * Time.deltaTime);
}
void LookRotation()
{
float rotX = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotX, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -60f, 60f);
fpsCam.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
}
}
I understand a lot of it, however not all of it at all. The part that most confuses me is just the gravity and falling/jumping. I understand why forwardSpeed does what it does, and why sideSpeed does what it does. Its just the math that confuses me. So, how can I fix my problem and or make my character controller better/easier to understand without making it feel basic. (It currently feels great aside from my gravity problem. Even with jumping. Its just the falling that is stumping me)
If you’re following the tutorial closely, then I think you need a different tutorial. (Had a look at the official ones?)
This script handles gravity by setting the y-velocity to some value when jump is pressed, and constantly subtracting (adding? Is Physics.Gravity.y set to a positive value?) a small amount from it each frame. The problem with this is that it will keep adding to it when you’re on the ground, allowing it to approach a ridiculous magnitude quickly. If you jump again, it’s not a problem because it gets set back to a reasonable value, but if you fall off an edge, you’ll move downward very, very quickly due to the accumulated velocity.
A quick fix is to only accelerate downward if the character is not grounded. I suspect that this will cause problems down the road, though, since gravity is not that simple.
Some concerned lecturing from me (sorry):
I see from another post you made that you want to be “vanilla” and script as much as possible. Learning scripting by exposure is commendable, but I wouldn’t suggest systematically removing every feature that Unity provides to make development easier just for the sake of it. I see this a lot, and I heartily recommend you reconsider and make due use of Rigidbody’s/Rigidbody2D’s, character controllers, etc. Given that you haven’t yet mastered the bit of scripting above, I’m very worried you’ll want to give up long before completing a project. Writing all but the most simple physics engines is a project and a half in itself, which is one big reason why Unity and other engines exist. Maybe make something using Unity’s physics first, then try your hand at writing some custom stuff?
The most vanilla way of making a game is not using an engine at all. That is not the best way!
Thanks! Idk, If I take out the gravity variable (not Physics.gravity.y) then it works fine. But, he has a really moon jumpy feel if I don’t add that extra downward pull for some reason
And no by all means lecture! By vanilla, I more or less mean not using pre made scripts and assets. Things like Character Controller and rigid body is okay! I just wanted to attempt it without rigid body so I could customize the physics later down the road.
I’m glad you’re open to using them. Rigidbody’s do make it pretty easy to isolate certain features; for example, you can use collision layers or certain functions to limit collisions between certain objects if desired, gravity can be disabled or scaled, and axes of motion can be constrained-- this allows you to customize behavior to a good degree. Premade scripts can be a similar story, but I do understand wanting to do it yourself. Just something to keep in mind.
This isn’t a perfect solution, since verticalVel is going to be whatever speed you last landed at when you fall off of a platform’s edge… You’d have to set it to 0 when landing or something like that. Setting it to 0 every frame you’re on the ground is probably no good, since it would complicate walking up or down slopes.
Consider using CharacterController.SimpleMove() to handle gravity for you. If you try a Rigidbody instead of a Character Controller, you can add a constant force to increase the gravity to ameliorate the floatiness, or just increase the global gravity in the Physics settings if you want everything to fall at that increased rate.
[edit] It Works! Ahh so obvious… Thanks a lot. Id still like your opinion on the rest tho.
Hm, ill definitly try that. And yeah the tutorial I was watching (quill18 or whatever), he started off with SimpleMove, but then went on about how it wouldn’t work well with something else that was added later on. I think it was perhaps something with the part that accelerates downwards instead of a constant speed.
Would using a Rigidbody be more efficient for a FPV game? Really, this place that I’ve made is specifically for messing around and just building up to learn things. I’ve done a lot of the tuts, but I don’t like how they have pre-made scripts as I said I didn’t like using. But I take plenty of notes and add comments when they explain
It’s hard to say what’s better for what, exactly. There’s nothing wrong with using a Character Controller, especially for first-person and third-person 3D games, which is mostly what it was intended for, when “more realistic” movement is not fun. However, I’ve had no trouble using a Rigidbody2D to get responsive, playable controls in a game where skillful movement is important, and if you want more complex interaction and functionality, Rigidbody’s are generally more integrated than Character Controllers and have a better feature set.
If you know for sure that there won’t be any complex motion or interation (seems like you’re pretty sure), Character Controller could work.
I was thinking that maybe that tutorial leaves problems in the script early on to keep it simple at first, then shows how to correct them as it goes along. I suppose I may have been too quick to judge-- a lot of tutorials do that.