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 vanessa101 · Jun 16, 2016 at 03:12 AM · movementscript.jump

This is frustrating

Dont understand what im doing wrong, I want my object to just simply jump whiles its moving towards the right by presing the spacebar and i want it so that the object jumps over other obstacles coming towards it. I want the player to use the spacebar to make the object jump over the obstacle basically. And i want the jump to be able to jump again. Not just one jump and thats it.

Right now i have this script and the objects moves towards the right yes, but when i press the spacebar for it to jump, it just falls down and off the screen and through my ground. I dont know what happened? is it a problem with gravity.. or does something have to be appliedd...? like a layer or some type of component idk? is it something to do with gravity... dont understand im new and learning i just want to be able to understand whats going on.

Script:

using UnityEngine; using System.Collections; using System;

public class Frog : MonoBehaviour { private Vector3 movement; private float ground; private float speed = 10f; private float jump_power = 15f; public float gravity = 5f;

 void Start()
 {
     ground = transform.position.y;
 }

 void Update()
 {
     movement.x = speed; // moving to the right
     if (transform.position.y == ground)
         movement.y = (Input.GetButton("Jump") ? jump_power : 0f); // jump
     else
         movement.y -= gravity; // gravity
     transform.Translate(movement * Time.deltaTime); // applies movement
 }

}

Comment
Add comment · Show 2
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 vanessa101 · Jun 16, 2016 at 03:18 AM 0
Share

i want the player to jump up and drop back down on the ground. not jump and fall through the screen n off the game wtf

avatar image NoseKills vanessa101 · Jun 16, 2016 at 05:39 AM 0
Share

Please post answers as answers and comments as comments. I converted this answer to a comment for you this time...

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by vanessa101 · Jun 16, 2016 at 07:52 AM

okay so what are you saying i should add? I added the fixed update and your code but nothing happened. the object still dropped when i press spacebar to make it jump. seems like it just moves to the right but once the spacebar is pressed , thats the end it just falls off screen

Comment
Add comment · Show 1 · 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 NoseKills · Jun 16, 2016 at 04:33 PM 0
Share

This is not a good ANSWER for this question @vanessa101.

Please post comments as comments under whatever you want to comment on.

$$anonymous$$y answer has a suggestion for the condition you might want to add. You can't trust that the object's y-position will hit exactly y==0 when it comes back down so you need to adjust the position if it goes under ground.

avatar image
0

Answer by NoseKills · Jun 16, 2016 at 06:07 AM

 if (transform.position.y == ground)
          movement.y = (Input.GetButton("Jump") ? jump_power : 0f); // jump
      else
          movement.y -= gravity; // gravity


The only thing keeping the object from sinking is that you set movement.y to 0 if the object is at a right y-coordinate and 'jump' is not pressed.

You apply the movement to the object using Time.deltaTime which depends on your framerate.

So lets say you press jump and movement.y becomes 15 (jump_power). Then transform.Translate(movement * Time.deltaTime); changes the y position of the object to let's say 0.94 (depends on deltatime). In the next Update()s the object drops by Time.deltaTime * gravity, let's say first it drops by 0.31 then by 0.33 then by 0.31. Now the y position of the object is -0.01 and there isn't anything to stop it from falling.

Somewhere you need to add a condition like if (transform.position.y < 0) transform.position = new Vector3(transform position.x, 0, transform position.z or make some other way to keep the object above ground.

In theory your code could work by using FixedUpdate instead of Update because that would make Time.deltaTime constant and because your jump_power is exactly 3*gravity, the object should mathematically land on y==0 height in some Update(). Unfortunately you can't trust floating point numbers to be exactly what you expect. Y could still become -0.0000001 and break the system.

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

70 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

Related Questions

Help with jumping script 2 Answers

Need help with jumping on third person movement script. 0 Answers

Cant Move towards Right and Jump at same time? 2 Answers

I wrote a jumping script, but it isn't working and i'm not even getting any error codes. pls help. 0 Answers

Help: Use a Trigger object to slow Player while inside object 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