- Home /
Jump Script being extremely "sticky", performance issue?
I've been following a tutorial series for First and Third person controllers, but I can not get my jump working correctly. I've seen other scripts where people set their jump up like mine, but mine doesn't have the intended behaviour. When I reset my verticalVelocity float to 0.0f, it seems to cause a major performance issue, and it takes me 5 or 6 key presses to actually jump (hence it being "sticky"). If I don't reset my verticalVelocity float, then every time I fall off of an object, I keep adding more and more gravity each fall, until I drop so fast it looks like I teleport.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {
public int movementSpeed;
public int mouseSensitivity;
public float UDClamp;
public float jumpSpeed;
float verticalVelocity = 0.0f;
float rotUD = 0;
CharacterController cc;
void Start() {
Screen.lockCursor = true;
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//Rotation
float rotLR = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLR, 0);
rotUD -= Input.GetAxis("Mouse Y") * mouseSensitivity;
rotUD = Mathf.Clamp(rotUD, -UDClamp, UDClamp);
Camera.main.transform.localRotation = Quaternion.Euler(rotUD, 0, 0);
//Movement
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float strafeSpeed = Input.GetAxis("Horizontal") * movementSpeed;
if(!cc.isGrounded) {
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
if(cc.isGrounded) {
//verticalVelocity = 0.0f;
if(Input.GetButtonDown("Jump")) {
verticalVelocity = jumpSpeed;
}
}
Vector3 speed = new Vector3(strafeSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
cc.Move(speed * Time.deltaTime);
}
}
The commented out line where I set verticalVelocity to 0.0f is the one in question. Can anyone tell me why this is happening, or give me any insight on how to fix it? Thank you.
Answer by pol315 · Oct 23, 2013 at 07:24 PM
This may not be the best solution to the problem but I fixed this one on my own. IF I get a better answer I will mark it as solved. The way that I did it was to create two booleans, one for jumpng, and one for falling. If the player is jumping, apply gravity as normal, but if the player walks off of a ledge and is not jumping, then set the gravity to 0.0f, then set falling to true so that you can then apply gravity to a falling player. Here's the code containing the answer :
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {
public int movementSpeed;
public int mouseSensitivity;
public float UDClamp;
public float jumpSpeed;
float verticalVelocity = 0.0f;
float rotUD = 0;
CharacterController cc;
bool Jumping;
bool Falling;
void Start() {
Screen.lockCursor = true;
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//Rotation
float rotLR = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLR, 0);
rotUD -= Input.GetAxis("Mouse Y") * mouseSensitivity;
rotUD = Mathf.Clamp(rotUD, -UDClamp, UDClamp);
Camera.main.transform.localRotation = Quaternion.Euler(rotUD, 0, 0);
//Movement
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float strafeSpeed = Input.GetAxis("Horizontal") * movementSpeed;
if(!cc.isGrounded) {
if(!Jumping && !Falling) {
verticalVelocity = 0.0f;
Falling = true;
}
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
if(cc.isGrounded) {
if(Jumping) {
Jumping = false;
}
Falling = false;
if(Input.GetButtonDown("Jump")) {
verticalVelocity = jumpSpeed;
Jumping = true;
}
}
Vector3 speed = new Vector3(strafeSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
cc.Move(speed * Time.deltaTime);
}
}
If it works as you like, it's all good :)
Out of curiosity, did you figure out what was causing your Left-Curve exponential speed gain? I'll give you a thumbs up if you did ;)
That's actually the way that I wanted it to behave. I did a little bit of investigating using the two graphs that you posted and it helped me understand how the Unity physics work a little bit better though! $$anonymous$$y main problem was just that the jump functionality was blocked the way that I had it previously. I will admit I have a lot more to learn about the physics yet though :P Thanks for your help!
Well, you can have it for the effort :)
Another thing that is good for jump which perhaps gets overlooked is AddForce or AddRelativeForce Force$$anonymous$$ode.Impulse
You should have enough $$anonymous$$arma to vote on Questions, Answers and Comments now.
Enjoy :)