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 /
This question was closed Dec 01, 2017 at 05:53 AM by vadurshan for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by vadurshan · Nov 30, 2017 at 10:30 AM · unity 5scriptingbasics

Issues with jump control for player

I created a movement script for a player (cube). The movement seems to working fine but the jump control seems to be messed up.

What I wanted : To make the cube jump once until it collides with a surface.

Whats I got : The cube jumps once when I press the jump key (space) and when I hit the space key multiple times the cube does a double jump.

I would also like to know if there is any better way to write a movement script.

Thank you in advance.

Code :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ball_movement : MonoBehaviour {

 public float speed;
 public float jumpforce;
 private bool isground;
 Rigidbody rb;
 Vector3 jump;

 void Start()
 {
     isground = true;
     rb = GetComponent<Rigidbody> ();
     jump = new Vector3 (0f, jumpforce, 0f);
 }

 void OnCollisionStay(Collision other)
 {
     if(other.gameObject.CompareTag("Ground"))
         isground = true;
     
 }

 void Update()
 {
         float movex = Input.GetAxis("Horizontal");
         float movey = Input.GetAxis("Vertical");
         this.transform.Translate (movex * speed * Time.deltaTime, 0f, movey * speed * Time.deltaTime);

     if(Input.GetKeyDown(KeyCode.Space) && isground)
         {
         rb.AddForce (jump*jumpforce, ForceMode.Impulse);
         //rb.velocity = jump;
         isground = false;
         }
 }

}

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

  • Sort: 
avatar image
0
Best Answer

Answer by Thaun_ · Nov 30, 2017 at 10:49 AM

It is probably cause the isGround is setting to true just after you jump. What you could do is to check if your Y Velocity is 0.

 if(GetComponent<Rigidbody>().velocity.y <= 0){
     isground = true;
 }else{
     isground = false;
 }

(On Update) or just simply,

 isground = GetComponent<Rigidbody>().velocity.y <= 0;


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

Follow this Question

Answers Answers and Comments

149 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

Related Questions

How to make an enemy ragdoll on death? 1 Answer

Changing scenes without a new script? 1 Answer

Adding animation from other model on character 0 Answers

Must I attach every script to a gameobject in order to work ? 2 Answers

Can I make Money collecting script without attaching it to a object ? I tried but I got error 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