- Home /
Need help limiting Air Movement
I'm making an fps and I need help with the movement. Everything seems to be working fine except I'm trying get it to where the player keeps their momentum after they jump while also being able to control their movement in air slightly, any help would be awesome as I'm new to scripting, also if there's anything I should change just let me know please. (:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
CharacterController cc;
float xSpeed;
float zSpeed;
Vector3 moveVector;
float vVelocity;
public float gravity = 1F;
public float moveSpeed = 7f;
public float jumpSpeed = 7;
public float runMulti = 1.5f;
public string jump = "Jump1";
public string horizontal = "HorizontalPad1";
public string vertical = "VerticalPad1";
public string run = "Run1";
private CollisionFlags m_CollisionFlags;
private CharacterController m_CharacterController;
// Use this for initialization
void Start ()
{
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButton(run))
Run();
else
{
Walk();
}
}
void Walk()
{
xSpeed = Input.GetAxis(horizontal);
zSpeed = Input.GetAxis(vertical);
moveVector = new Vector3(xSpeed, 0, zSpeed);
moveVector = transform.TransformDirection(moveVector) * moveSpeed;
vVelocity += Physics.gravity.y * gravity * Time.deltaTime;
if (cc.isGrounded)
{
if (Input.GetButtonDown(jump))
{
vVelocity = jumpSpeed;
}
}
if (!cc.isGrounded)
{
moveVector = new Vector3(xSpeed, 0, zSpeed);
moveVector = transform.TransformDirection(moveVector) * moveSpeed;
}
moveVector.y = vVelocity;
m_CollisionFlags = cc.Move(moveVector * Time.deltaTime);
}
void Run()
{
xSpeed = Input.GetAxis(horizontal);
zSpeed = Input.GetAxis(vertical);
moveVector = new Vector3(xSpeed, 0, zSpeed);
moveVector = transform.TransformDirection(moveVector) * moveSpeed * runMulti;
vVelocity += Physics.gravity.y * gravity * Time.deltaTime;
if (cc.isGrounded)
{
if (Input.GetButtonDown(jump))
{
vVelocity = jumpSpeed;
}
}
if (!cc.isGrounded)
{
moveVector = new Vector3(xSpeed, 0, zSpeed);
moveVector = transform.TransformDirection(moveVector) * moveSpeed;
}
moveVector.y = vVelocity;
m_CollisionFlags = cc.Move(moveVector * Time.deltaTime);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
//dont move the rigidbody if the character is on top of it
if (m_CollisionFlags == CollisionFlags.Below)
{
return;
}
if (body == null || body.isKinematic)
{
return;
}
body.AddForceAtPosition(cc.velocity * 0.1f, hit.point, ForceMode.Impulse);
}
}
the code you posted is just a red mess of nonsense, at least on my computer.
multiply the movement vector by a value below 1.0 while in the air (0.5 would mean half of the normal control).
@cherno Sorry about that I didn't realize i posted the code incorrectly. I edited the post, I appreciate any help I can get, Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612034156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I introduce a turning function for my player in this script? 1 Answer
Modify a script to use buttons instead of keys 1 Answer
Moving gameobject by GUI buttons 1 Answer
Position Text inside Canvas via Script 2 Answers
How to make an object move with a script which isn't attached to it? 2 Answers