- Home /
Player object falling slowly when holding input
Hello! I've recently encountered a problem with my player GameObject. When I hold other buttons while it's falling, it falls MUCH slower than normal. I don't want this, is there some way to fix that? But, I still want to be able to change direction while falling. Here is my script:
var MoveSpeed : float = 100f;
var RunSpeed : float = 200f;
var Forward : float = 150f;
var ForwardRun : float = 300f;
var JumpHeight : float = 7500f;
var Jumped = false;
function Start ()
{
rigidbody.freezeRotation = true;
}
function Update ()
{
if(Input.GetKey(KeyCode.Space) && !Jumped)
{
Jumped = true;
rigidbody.AddForce(Vector3.up * JumpHeight * Time.deltaTime);
}
//Player movement
if(Input.GetKey(KeyCode.W))
rigidbody.velocity = transform.forward * Forward * Time.deltaTime;
if(Input.GetKey(KeyCode.S))
rigidbody.velocity = transform.forward * -MoveSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.D))
rigidbody.velocity = transform.right * MoveSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.A))
rigidbody.velocity = transform.right * -MoveSpeed * Time.deltaTime;
//Player run movement
if(Input.GetKey(KeyCode.LeftShift))
{
if(Input.GetKey(KeyCode.W))
rigidbody.velocity = transform.forward * ForwardRun * Time.deltaTime;
if(Input.GetKey(KeyCode.S))
rigidbody.velocity = transform.forward * -RunSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.D))
rigidbody.velocity = transform.right * RunSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.A))
rigidbody.velocity = transform.right * -RunSpeed * Time.deltaTime;
}
}
function OnTriggerStay ()
{
Jumped = false;
}
I'm using tbkn's given script ins$$anonymous$$d to fix this problem ins$$anonymous$$d, located on my other question.
Answer by slek120 · Apr 06, 2014 at 12:13 PM
What is happening is that the velocity isn't accelerating downwards. You set rigidbody.velocity.z to 0 every frame. There are many ways to solve this problem, here is one:
float fallVelocity = rigidbody.velocity.z;
//Player movement
if(Input.GetKey(KeyCode.W))
rigidbody.velocity = transform.forward * Forward * Time.deltaTime;
if(Input.GetKey(KeyCode.S))
rigidbody.velocity = transform.forward * -MoveSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.D))
rigidbody.velocity = transform.right * MoveSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.A))
rigidbody.velocity = transform.right * -MoveSpeed * Time.deltaTime;
//Player run movement
if(Input.GetKey(KeyCode.LeftShift))
{
if(Input.GetKey(KeyCode.W))
rigidbody.velocity = transform.forward * ForwardRun * Time.deltaTime;
if(Input.GetKey(KeyCode.S))
rigidbody.velocity = transform.forward * -RunSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.D))
rigidbody.velocity = transform.right * RunSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.A))
rigidbody.velocity = transform.right * -RunSpeed * Time.deltaTime;
}
rigidbody.velocity += Vector3.down * fallVelocity;
Some suggestions I would like to make. Physics calculations are best done in FixedUpdate. Input.GetAxis makes it easy so that you don't have to do all these if's. Hope this helps.
Wait, when I try to put rigidbody.velocity.y/x/z, it gets an error. It says that it can't convert Vector3 to a float. Any way to fix that?
I'm sorry. I made a few mistakes when typing the answer. I did rigidbody.velocity.z ins$$anonymous$$d of y and I used Vector3.down ins$$anonymous$$d of up. I don't know where you are getting your error. Can you tell me what line you get that error in?
I wanted to show you how unity does character control in sample assets. If you download sample assets and open Assets/Sample Assets/2D/Scenes/2D Character Controller, you will see how unity handles input. Here is a version I wrote that might be easier to understand.
using UnityEngine;
using System.Collections;
public class CharacterControl : $$anonymous$$onoBehaviour
{
public float moveSpeed = 2;
public float run$$anonymous$$ultiplier = 2;
public float forward$$anonymous$$ultiplier = 1.5f;
public float jumpForce = 4;
bool jump = false;
bool grounded = true;
void Update ()
{
// Read the jump input in Update so button presses aren't missed
// In Unity, key presses are reset every update
if (Input.GetButtonDown ("Jump")) { // This is space by default
jump = true;
}
}
void FixedUpdate ()
{
float xAxis = Input.GetAxis ("Horizontal"); // [-1,1] A, D or Left, Right
float yAxis = Input.GetAxis ("Vertical"); // [-1,1] S, W or Down, Up
bool run = Input.GetButton ("Fire1"); // This is left ctrl by default. To change this, go to Edit->Project Settings->Input
// Send inputs to move function
$$anonymous$$ove (xAxis, yAxis, jump, run);
// Reset the jump input once it has been used.
jump = false;
}
void OnTriggerStay ()
{
grounded = true;
}
void $$anonymous$$ove (float xAxis, float yAxis, bool jumped, bool run)
{
// Check if moving forward
if (yAxis > 0) {
yAxis *= forward$$anonymous$$ultiplier;
}
// Convert to relative axis
Vector3 velocity = transform.forward * yAxis + transform.right * xAxis;
// Calculate velocity
velocity *= moveSpeed;
if (run) {
velocity *= run$$anonymous$$ultiplier;
}
// Add current falling velocity
velocity += Vector3.up * rigidbody.velocity.y;
// Set new velocity
rigidbody.velocity = velocity;
// Jump
if (grounded && jumped) {
rigidbody.AddForce (Vector3.up * jumpForce, Force$$anonymous$$ode.Impulse);
grounded = false;
}
}
}
One difference between my version and yours is that directions aren't exclusive. You can hold W and D to go forward right. Also, If you hold A and D, $$anonymous$$e doesn't move where yours would go left.
Yeah, I know about the Input.GetAxis and as well as GetButton. If I remember correctly, the controls are configured in the Project Settings, right? Correct me if I'm wrong. Oh, and I realized that I had the error because I was using rigidbody.velocity.z on the if command itself.
I actually tried rigidbody.velocity.y ins$$anonymous$$d of z but I didn't change the Vector3 direction, thinking it would be like gravity. I also tried doing var "FallVelocity : float = rigidbody.velocity.z" ins$$anonymous$$d of "float FallVelocity = rigidbody.velocity.z" but still didn't work.
You can't set that outside of a function. Are you setting the FallVelocity variable to rigidbody.velocity.z in Update?
Answer by OscL1 · Apr 18, 2020 at 10:45 PM
It looks like your question have been solved, but I just want to say that there is a better way for writing player movement. It will safe time, and run better.
float speed = 30.0f;
void Update()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 move = (hor * speed * Time.deltaTime) + (ver * speed * TIme.deltaTime);
transform.Translate(move);
}
Your answer
Follow this Question
Related Questions
Change rigidbody's jumping speed 2 Answers
jump and land quicker 2 Answers
A* Rigidbody. Aron Granberg 1 Answer
How to program gravity around a cylinder? (Vine growing up a tree) 1 Answer
Jump Input Sensitivity 0 Answers