Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 deeredman1991 · Jul 11, 2011 at 05:03 PM · c#movementjumpingwalkingrunning

Why can't I jump and run at the same time? c#

Hey guys I wrote a c# script for running and jumping...unfortunately it doesn't allow me to run and jump at the same time...I can run and I can jump but not both at the same time anyone mind helping me out on this one? :)

 int isstuned = 0;
 
 
     //if the player isn't stunned
             if(isstuned == 0)
             {
                 //if space is pressed and the ray is reading a collision is down or if up arrow is pressed and the ray is reading a collision of down
                 if(Input.GetKeyDown("space") && Physics.Raycast(transform.position, -transform.up,1) || Input.GetKeyDown(KeyCode.UpArrow) && Physics.Raycast(transform.position, -transform.up,1))
                 {
                     //make the player transform up by the speed of jumpforce using an impulse type force
                     rigidbody.AddRelativeForce(transform.up * jumpforce, ForceMode.Impulse);
                 }
                 //if a or left arrow is pressed
                 if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
                 {
                     //push left
                     rigidbody.velocity = -transform.right * curspeed;
                 }
                 //if d or right arrow is pressed
                 if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
                 {
                     //push right
                     rigidbody.velocity = transform.right * curspeed;
                 }
             }
Comment
Add comment
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

1 Reply

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

Answer by almo · Jul 11, 2011 at 05:25 PM

Maybe it's because your

 rigidbody.velocity = transform.right * curspeed;

Sets the speed. It doesn't add to it. I'm guessing that whatever you're doing in the jump code gets overridden by the run code.

Also, not related to this problem, you want this

 if((Input.GetKeyDown("space") || Input.GetKeyDown(KeyCode.UpArrow)) && Physics.Raycast(transform.position, -transform.up,1))

not this

  if(Input.GetKeyDown("space") && Physics.Raycast(transform.position, -transform.up,1) || Input.GetKeyDown(KeyCode.UpArrow) && Physics.Raycast(transform.position, -transform.up,1))

Your code does a redundant Raycast, and these are expensive.

Comment
Add comment · Show 5 · 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 deeredman1991 · Jul 11, 2011 at 05:40 PM 0
Share

thank you for the raycast to improve my jump code :)...but unfortunately it doesn't work when I use "rigidbody.velocity += transform.right * curspeed;" either...:( so I don't think that's the problem :( plus isn't addrelativeforce and velocity two different things?

avatar image almo · Jul 11, 2011 at 06:07 PM 0
Share

I'm not entirely sure. But I see nothing else wrong with the code. Try using addrelativeforce for the other movements, and see what happens. It may not give the motion you want, but if you can the jump and run, you know that's the issue.

avatar image deeredman1991 · Jul 11, 2011 at 06:20 PM 0
Share

yep it was cause I was using velocity...but now I accelerate out of control lol...I think I can somehow fix it though thanks :D

avatar image Dreamblur · Jul 11, 2011 at 06:21 PM 0
Share

@deeredman1991 AddRelativeForce applies a force to the rigidbody relative to its orientation. During a physics step, all distributed forces (including automatic forces) acting on a single rigidbody are computed, and its velocity is changed based on the resultant net force. When someone applies a force to a rigidbody, he/she usually does so with the intention of changing the velocity. Applying a force to a rigidbody and setting its velocity to an absolute value are just 2 sides of the same coin for most intents and purposes.

In your case, you are setting the rigidbody's velocity to an absolute value after applying an impulse force to it. Distributed forces (Force$$anonymous$$ode.Force and Force$$anonymous$$ode.Acceleration) are only calculated during the next physics step so you can set the rigidbody's velocity prior to that and still fulfill your objective. However, impulse forces (Force$$anonymous$$ode.Impulse and Force$$anonymous$$ode.VelocityChange) are not "real forces" in the sense that they are immediately calculated into the rigidbody's velocity as soon as they are applied. Since you're setting the velocity to an absolute value after the impulse force is applied, any net change done by the impulse force to the velocity is nullified.

Further notes about your script:

Like almo mentioned, you're raycasting needlessly. Try to $$anonymous$$imize the raycasts whenever possible.

Set up virtual button axes ins$$anonymous$$d of accessing keycode events. Hardcoded input methods are quite sloppy and rarely necessary.

Proper use of the if-else conditional will save you a few hours of unwanted debugging.

Do NOT use AddRelativeForce and the Transform local space vectors (transform.right, transform.up, transform.forward) together. Going to the right of your right side is not the same as going to your right side. A slight rotation of your object will show you what I mean.

avatar image deeredman1991 · Jul 11, 2011 at 06:54 PM 0
Share

see Dremblur when I use the Input.GetAxis("Horizontal") my player likes to run right through colliders on occasion but maybe it's because the only way I know how to move the player after that is by using transform :(...the only way I saw to fix this was to use hardcoded input methods lol...I'm sure there is a better solution but I don't know what it is...however if you would be willing to shed some light on my predicament I would be eternally grateful :)

this is my code using the x axis

float wx = Input.GetAxis("Horizontal") Time.deltaTime curspeed;

transform.Translate(wx, 0, 0);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Movement Jumping Help 0 Answers

CharacterController Jump from moving platform. 0 Answers

a better movement code C# 3 Answers

Player Falls Much Slower While Moving 2 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