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 /
  • Help Room /
avatar image
0
Question by polaroceanfire · Oct 06, 2015 at 05:28 AM · errormovement

Issue with Jumping and Movement

Hi, I'm trying to make movement where the character can go freely left and right but can only jump so far upwards (and not infinitely, like GetAxisRaw would allow). I'm having an issue with line 25 wherein jumping is "bool" and not "float", and I'm not sure how to correct this issue to still allow only one jump press at a time and avoid infinite jumping. I'm pretty new to coding so sorry if this is something obvious.

 using UnityEngine;
 using System.Collections;
 
 public class VelocityMove : MonoBehaviour {
 
     // UPDATED: Now Vector2 variables
     public Vector2 velocity = new Vector2(15f,15f); // Used to store velocity
     private Vector2 direction; // Used to store input directions (-1, 0, or 1)
 
     public Rigidbody2D rb2D; // This gameObject's Rigidbody2D component
 
     // Use this for initialization
     void Start ()
     {
         // Store reference to the Rigidbody 2D attached to this gameObject
         rb2D = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         // More responsive input in Update()
         // Use Input Manager for input (-1 for left, 1 for right, 0 for no movement)
         direction.x = Input.GetAxisRaw ("Horizontal");
         direction.y = Input.GetKeyDown (KeyCode.W);
     }
 
     // Update used for physics
     void FixedUpdate()
     {
         rb2D.velocity = Vector2.Scale(velocity, direction);
     }
 }

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
0

Answer by Statement · Oct 10, 2015 at 08:07 PM

So your compile error is because you are trying to assign a bool to a float. You can convert it to a float like this:

 if (Input.GetKeyDown(KeyCode.W))
     direction.y = 1f;
 else 
     direction.y = 0f;
 
 // ... Or ...
 
 direction.y = Input.GetKeyDown(KeyCode.W) ? 1f : 0f;

But this will likely not give you any nice effect since your direction.y will get set to 0 the next frame after pressing W.

I'll present you a modified version of your script that hopefully should give you a sense of how this works. I no longer update in Update but moved all to FixedUpdate. No longer setting the velocity directly but rather applying impulses to the rigidbody. This way we can also let the physics engine apply gravity over time.

 using UnityEngine;
 
 public class VelocityMove : MonoBehaviour
 {
     public float speed = 1f;
     public float jumpForce = 5f;
     private Rigidbody2D rb2D;
 
     void Start()
     {
         rb2D = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate()
     {
         Vector2 force = Vector2.zero;
 
         force.x = Input.GetAxisRaw("Horizontal") * speed;
         force.y = Input.GetKeyDown(KeyCode.W) ? jumpForce : 0;
 
         // Add force instead of adjusting velocity manually.
         rb2D.AddForce(force, ForceMode2D.Impulse);
     }
 }
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

32 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2 error messages i can't seem to fix (CS0101 and CS0111) 1 Answer

How touch works 1 Answer

I have an error with my scripts PLEASE HELP ME 2 Answers

Moving And Rotation 2D Object (Touch) 0 Answers

Camera moves well in game preview but not so well in actual game 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