Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darwin Mecharov · Apr 06, 2014 at 05:52 AM · movementrigidbodyinputgravityjump

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;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darwin Mecharov · Apr 08, 2014 at 04:47 PM 0
Share

I'm using tbkn's given script ins$$anonymous$$d to fix this problem ins$$anonymous$$d, located on my other question.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darwin Mecharov · Apr 07, 2014 at 09:18 AM 0
Share

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?

avatar image slek120 · Apr 07, 2014 at 03:40 PM 2
Share

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.

avatar image Darwin Mecharov · Apr 08, 2014 at 12:58 AM 0
Share

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.

avatar image Darwin Mecharov · Apr 08, 2014 at 01:08 AM 0
Share

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.

avatar image william9518 · Apr 08, 2014 at 01:33 AM 0
Share

You can't set that outside of a function. Are you setting the FallVelocity variable to rigidbody.velocity.z in Update?

Show more comments
avatar image
0

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);
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges