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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by brookeblood1 · Feb 13, 2013 at 12:54 AM · gravityjumpfirst person

First Person Jump Script Not Working

Hello all! I am working on my first Unity game, and I'm having some trouble.

I want my object to jump on a "W" key press, but I cannot get it to work.

Here is my code: (Javascript)

 var jumping : boolean = false;
 var timer=5;
 
 function Update () {

     //Should skip this before it is pressed
     if (jumping == true){
         timer+=-.1 * Time.deltaTime;
     }

     //Should skip this before it is pressed
     if (timer == 0){
         timer=5;
         jumping = false;
 
     if (Input.GetAxis("Vertical") > 0 && jumping == false) {
 
         transform.Translate(0, 10 * Time.deltaTime * Input.GetAxis("Vertical"),0);
         jumping = true;
     }
     
 
     }
     
     //transform.Translate(0,0, 100 * Time.deltaTime);
 }

If I am way off base on how to do this, what is an easier way?

I'd like to stay away from character controllers to do this.

Here is a picture of what I'm thinking.

alt text

jumping block.png (51.7 kB)
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 iwaldrop · Feb 13, 2013 at 04:35 AM

Just because you want to stay away from the Character Controller doesn't mean you can't take advantage of physics.

Attach a rigidbody and collider to your GameObject (if you haven't already). Then, in your code, add the following

 public string walkableTag;
 
 void Update()
 {
     if (!jumping && Input.GetAxis("Vertical") > 0)
     {
         rigidbody.AddForce(Vector3.up * 10, ForceMode.Impulse);
         jumping = true;
     }
 }
 
 
 void OnCollisionEnter(Collision collision)
 {
     foreach (ContactPoint c in collision.contacts)
     {
         if (c.otherCollider.tag == walkableTag)
             jumping = false;
     }
 }

Alternatively you could use

 if (!jumping && Input.GetButtonDown("Jump"))
Comment
Add comment · Show 3 · 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 brookeblood1 · Feb 13, 2013 at 05:00 AM 0
Share

The code you provided basically makes the jumping work, but I have run across another problem.

When I add the line that I had commented out in the original code, the object accelerates, yet bounces off on a collision with the floor.

 transform.Translate(0,0, 100 * Time.deltaTime);

How can I keep the object on the floor when it is just accelerating, and allow it to jump freely using your code?

avatar image iwaldrop · Feb 13, 2013 at 06:04 AM 0
Share

Add this line in your awake

 void Awake()
 {
     rigidbody.freezeRotation = true;
 }
avatar image brookeblood1 · Feb 13, 2013 at 11:57 AM 0
Share

Got it, thank you very much!

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

10 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

Related Questions

Adding a jump feature help? 2 Answers

Switching gravities not that effective. Need help 1 Answer

Jumping a specific height using Velocity & Gravity 1 Answer

Simple Circular jump along local normal- similar to planetary gravity 0 Answers

Getting this character controll script working right 1 Answer


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