- Home /
Question by
TH3HCK3ABL3 · Jul 20, 2013 at 08:08 AM ·
jumpspeedtime.deltatimemomentum
Preventing air-strafing has caused my character to become a small super hero, how do I fix this?
For some reason my characters gained superspeed when trying to separate the variables for movement and adjust the strafing to be very minimal. Any ideas on how to resolve this issue? Improve the script in anyway? Thanks.
Script:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class P1Script: MonoBehaviour {
public float nSpeed = 6.0f; // Units per second
public float speedM = .8f; // Multiplies the "nSpeed" variable to increase movement speed
public float airSpeed = 1.2f; // Multiplies the "cAirSpeed" variable while in the air
private float cSpeed; // The current Speed
float cAirSpeed; // The variable for nerfing air-strafeing
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;
cAirSpeed = cSpeed / 2;
cSpeed = cAirSpeed;
}
}
// X-Axis Look
float rotLeftRight = Input.GetAxis ("Mouse X");
transform.Rotate(0, rotLeftRight, 0);
// Forward
var velFor = transform.forward * Input.GetAxis ("Vertical") * cSpeed;
if(!cController.isGrounded) {
if(!Input.GetKeyDown ("s")) {
velFor = transform.forward * Input.GetAxis ("Vertical") * cAirSpeed;
} else {
velFor = transform.forward * Input.GetAxis ("Vertical") * cSpeed;
}
}
// Backwards
var velBack = transform.forward * Input.GetAxis ("Vertical") * cSpeed;
if(!cController.isGrounded) {
if(!Input.GetKeyDown ("w")) {
velBack = transform.forward * Input.GetAxis ("Vertical") * cAirSpeed;
} else {
velBack = transform.forward * Input.GetAxis ("Vertical") * cSpeed;
}
}
// Y-Axis Look
vRotation -= Input.GetAxis ("Mouse Y");
vRotation = Mathf.Clamp(vRotation, -yRange, yRange);
Camera.main.transform.localRotation = Quaternion.Euler(vRotation, 0, 0);
// Right
var horR = transform.right * Input.GetAxis ("Horizontal") * cSpeed;
if(!cController.isGrounded) {
if(!Input.GetKeyDown ("d")) {
horR = transform.right * Input.GetAxis ("Horizontal") * cAirSpeed;
} else {
horR = transform.right * Input.GetAxis ("Horizontal") * cSpeed;
}
}
// Left
var horL = transform.right * Input.GetAxis ("Horizontal") * cSpeed;
if(!cController.isGrounded) {
if(!Input.GetKeyDown ("d")) {
horL = transform.right * Input.GetAxis ("Horizontal") * cAirSpeed;
} else {
horL = transform.right * Input.GetAxis ("Horizontal") * cSpeed;
}
}
vSpeed -= gravity * Time.deltaTime; // Applies gravity acceleration to the vertical speed
// Forwards Momentum
velFor.y = vSpeed; // include vertical speed in the velFor
cController.Move (velFor * Time.deltaTime); // Convert velFor to displacement and move the character
// Backwards Momentum
velBack.y = vSpeed; // include vertical speed in the velBack
cController.Move (velBack * Time.deltaTime); // Convert velBack to displacement and move the character
// Right Momentum
horR.y = vSpeed; // Applies gravity acceleration to the right horizontal speed:
cController.Move (horR * Time.deltaTime); // Convert horR to displacement and move the character
// Left Momentum
horL.y = vSpeed; // Applies gravity acceleration to the left horizontal speed:
cController.Move (horL * Time.deltaTime); // Convert horL to displacement and move the characte
//Sprinting
if (Input.GetButton ("Sprint")) { // Check for "Sprint" as defined by project inputs
cSpeed = nSpeed * speedM;
} else {
cSpeed = nSpeed;
}
}
}
Comment
To prevent air-strafing you could simply have just disabled the inputs needed to allow movement in s$$anonymous$$d of a mess of if statements, which reading is very confusing and not really needed. $$anonymous$$odify simple variables then at the very end put all the variables together to make the player move.
Your answer
Follow this Question
Related Questions
Problem with jump bool. Help?! 0 Answers
Problems with jumping script. 1 Answer
speed * Time.deltaTime = 0? 1 Answer
Problem with jumping and frames per second 4 Answers
Unity 3.4.0 Running really slow 1 Answer