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 SolunaGD · Jun 30, 2016 at 01:50 AM · c#jumpingjumping object

How to set a limit on how high a player can jump?

I am a beginner making a 3d platforming game where you have to grab coins and jump onto platforms to grab the coins out of reach from the ground. So I put together code which allows the player to jump. I tried setting a limit to how high they can jump but this prevents them from jumping while on the platforms which are higher that the jump limit. If I try to increase the height limit, this allows the player to jump higher, which I do not want. Here's my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 public class PlayerController : MonoBehaviour 
 {
     
     private int points;
     private Rigidbody rb;
     public float speed;
     public Text scoredis;
     public Text winner;
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         points = 0;
         PointDisplay();
         winner.text = "";
     }
 
     void FixedUpdate()
     {
         if(gameObject.transform.position.y <= 8)
         {
             float movejump = Input.GetAxis("Jump");
             float moveHorizontal = Input.GetAxis("Horizontal");
             float moveVertical = Input.GetAxis("Vertical");
             Vector3 movement = new Vector3(moveHorizontal, movejump, moveVertical);
             rb.AddForce(movement * speed);
         }
 
     }
     void OnTriggerEnter(Collider other) 
     {
         
         if (other.gameObject.CompareTag("Pickup"))
         {
             other.gameObject.SetActive(false);
             ++points;
             PointDisplay();
         }    
     }
 
     void PointDisplay()
     {
         scoredis.text = "Score: " + points.ToString();
         if (points >= 12)
         {
             winner.text = "You Win!";
         }
     }
 }
     

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 jgodfrey · Jun 30, 2016 at 04:25 AM

As you've realized, the jump "height" shouldn't have anything to do with the current "Y" position of the player,so you don't want this:

 if(gameObject.transform.position.y <= 8)

Instead, you probably want to control the value going into the AddForce() call to control the overall height of the jump. You might be able to get away with Mathf.Clamp'ing the Y value used in the force vector to a "sensible" range. Just figure out what force provides the maximum jump you want and then clamp the Y value of your vector to that max...

Comment
Add comment · Show 2 · 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 SolunaGD · Jul 01, 2016 at 04:03 AM 0
Share

Unfortunately, this does not solve my problem. What I think is that trying your clamp solution limits how high or low the movejump variable's value is. Even with the clamp, if the player holds down space, it will still add a force upward, but at a slower rate because of the clamp.

avatar image jgodfrey SolunaGD · Jul 01, 2016 at 09:32 PM 0
Share

I have no idea what your expected game mechanics are, but typically, you'd only allow a jump when the player is grounded and disallow it when they are already mid-jump (with the typical exception being a double-jump). So, the solution I presented should indeed limit the force applied to any single jump, though if you allow that force to be applied each time the space bar is pressed, you're not likely going to get the desired result..

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

172 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 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 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 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 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 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

How i make a ball jump where i point with my mouse?[3D] 0 Answers

Problems with jumping in Doodle Jump like game 2 Answers

Jumping only once (2D) 1 Answer

player not able to double jump 0 Answers

Need help with adding ground detection to my jump. (c#) (3D) 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