- Home /
How can I "nerf" air-strafe-ing in this script?
I want to be able to stop the Character Controller from continuing to move at the same rate in the air. Basically, lock it to whatever direction momentum during the jump and not allow you to suddenly go backwards from the direction you're currently going.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class P1Script: MonoBehaviour {
public float nSpeed = 5; // Units per second
public float speedM = .5f; // Multiplies the "nSpeed" variable to increase movement speed
public float airSpeed = 1.5f; // Multiplies the "cAirSpeed" variable while in the air
private float cSpeed; // The current Speed
public float turnSpeed = 5.0f; // Degrees per second
public float jumpSpeed = 8; // Height of Jump
public float gravity = 9.8f; // The rate at which gravity pulls down.
private float vSpeed = 0 ; // Current Vertical Velocity
float vRotation = 0; // Prevents Pitch(?)
public float yRange = 60.0f; // The angle limit for the Y-Axis look
CharacterController cController;
// Use this for initialization
void Start () {
Screen.lockCursor = true; // Locks Cursor to Screen; makes invisible
cController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//Jump
if(cController.isGrounded) {
vSpeed = 0; // Grounded character has vSpeed = 0...
if(Input.GetButton("Jump")) {
vSpeed = jumpSpeed;
var cAirSpeed = cSpeed / 2;
cSpeed = cAirSpeed;
}
}
// Rotation and Walk
float rotLeftRight = Input.GetAxis("Mouse X");
transform.Rotate(0, rotLeftRight, 0);
var vel = transform.forward * Input.GetAxis("Vertical") * cSpeed;
vRotation -= Input.GetAxis("Mouse Y");
vRotation = Mathf.Clamp(vRotation, -yRange, yRange);
Camera.main.transform.localRotation = Quaternion.Euler(vRotation, 0, 0);
var hor = transform.right * Input.GetAxis("Horizontal") * cSpeed;
vSpeed -= gravity * Time.deltaTime; // Applies gravity acceleration to the vertical speed
vel.y = vSpeed; // include vertical speed in the vel
cController.Move (vel * Time.deltaTime); // Convert vel to displacement and move the character
hor.y = vSpeed; // Applies gravity acceleration to the horizontal speed:
cController.Move (hor * Time.deltaTime); // Convert hor to displacement and move the character
//Sprinting
if (Input.GetButton("Sprint")) { // Check for "Sprint" as defined by project inputs
cSpeed = nSpeed * speedM;
} else {
cSpeed = nSpeed;
}
}
}
Answer by robertbu · Jul 18, 2013 at 11:43 PM
Try replacing line 41 with this:
var vel : Vector3;
if (!cController.isGrounded)
vel = transform.forward * cAirSpeed;
else
vel = transform.forward * Input.GetAxis("Vertical") * cSpeed;
Thank you so much! Just had to write that in C# for my script and it functions perfectly after I made cAirSpeed a normal Variable. Thank you again!
Answer by Slobdell · Jul 18, 2013 at 11:42 PM
Put a collider on the ground or whatever he's running on and if he's off the ground, keep him moving at the same speed. Basically, use a RayCast and check distance to the ground and only allow controls if he's within a certain distance. Or only allow movement controls if the player and the ground collider are colliding.
This sounds really useful. I will have to look into it. Probable for applying it to walls! Thank for your help!
Your answer
Follow this Question
Related Questions
Speed updated from another script, but doesn't persist 1 Answer
How to make object lag behind camera? 1 Answer
Make an object move towards another object when button is held then drift when button is released 0 Answers
Adding simple strafing to my character controller script... 0 Answers
Inspection camera script problem 2 Answers