I was searching for a script to copy and learn from when I stumbled upon this script, there are a few changes I want to make regarding gravity. Right now I just fall at the same speed until I hit the ground. I want it to act like “real life” where when you fall your speed increases over time.
(also if anyone has a good working movement script please tell me i’ve been searching for hours now.)
Search “Unity character controller” on YouTube or Google etc
A lot of people posted courses about it
It would also be a good starting point to refer to Asset Store’s asset
These are contents that have nothing to do with me personally
It’s just an example
and I recommend this asset.
To give you my personal tip, there are cases where you don’t necessarily have to buy an asset for analysis of an asset.
Read the documentation provided in the asset store.
Through it, it is possible to grasp the approximate design structure.
If you see it and like it or want to study it further, buy it.
As for the drop speed,
It doesn’t look like it’s written in the document,
I remember it was made of how much gravity would be affected, how much would it accelerate
For more information, contact asset developer
Your air drag is too high and is causing your character to fall very slowly. Try leaving drag at zero and adjust your move force according to the state of your character. But if you still feel you need more ground friction then you can add a physics material and adjust the friction.
And you don’t need a different code path for slopes. All ground can be considered a slope to some degree.
Here’s a very basic controller:
using UnityEngine;
public class SimpleRigidbodyController : MonoBehaviour // add this to a default capsule
{
Rigidbody rb;
void Start()
{
rb=GetComponent<Rigidbody>();
rb.freezeRotation=true;
}
void FixedUpdate()
{
Vector3 force=((transform.right*Input.GetAxisRaw("Horizontal"))+(transform.forward*Input.GetAxisRaw("Vertical"))).normalized;
if (Physics.SphereCast(transform.position,0.5f,-transform.up,out RaycastHit hit,0.55f)) // are we grounded?
{
force=Vector3.ProjectOnPlane(force,hit.normal)*6; // align input force to the ground and increase the force
if (Input.GetKey(KeyCode.LeftShift)) force*=2; // sprinter
if (Input.GetButton("Jump")) rb.AddForce(Vector3.up*6,ForceMode.Impulse); // jumper
}
rb.AddForce(force*5); // air movement force of 5
}
}
Thanks, I tried using your simple Character controller but upon trying it I build up speed extremely fast. How do I add a maximum speed value to the sprinting and walking? Or is there a better way to do that?
Or you can limit the velocity indirectly by limiting the force like this:
using UnityEngine;
public class SimpleRigidbodyController : MonoBehaviour
{
Rigidbody rb;
void Start()
{
rb=GetComponent<Rigidbody>();
rb.freezeRotation=true;
}
void FixedUpdate()
{
Vector3 force=((transform.right*Input.GetAxisRaw("Horizontal"))+(transform.forward*Input.GetAxisRaw("Vertical"))).normalized;
if (Physics.SphereCast(transform.position,0.5f,-transform.up,out RaycastHit hit,0.55f)) // are we grounded?
{
force=Vector3.ProjectOnPlane(force,hit.normal)*5; // align input force to the ground and increase the force
force-=rb.velocity*0.2f; // adjust the input force according to our current velocity. This will prevent us from moving too fast and also resists sliding around
if (Input.GetKey(KeyCode.LeftShift)) force*=2; // sprinter
if (Input.GetButton("Jump")) rb.AddForce(Vector3.up*6,ForceMode.Impulse); // jumper
}
rb.AddForce(force*5); // air movement force of 5
}
}