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 /
avatar image
0
Question by aman_jha · Jul 16, 2014 at 07:35 PM · c#physicsrigidbodytouchaddforce

Make a player jump tile

Hey guys,

What I have is a player that constantly moves forward. When the screen is touched, all of the tiles that are jump tiles respond. The regular tiles do nothing. What happens is that on a jump tile, if the player is on it, it bounces the player upwards to a constant height. It only bounces the player once.

What happens with the script below is that when the bounce () method is activated (external script) The player is shot up like normal, but is really weird, and near the peak of the ascent the player wiggles up and down a bit before finally coming down. Once it hits the ground, it shoots up again - regardless of whether or not the player is on top of a jumping tile. I don't have to touch the screen. The player just keeps bouncing as if he's on a giant trampoline.

It's worth noting that I have my own player physics enabled on the character, which means I'm not using rigidbody gravity. My diagnosis is that in the script, when Rigidbody.Addforce is called, it is called constantly and forever, and my coded gravity overrides it at some point, which allows the character to fall. This would explain the strange phenomenon where near the peak of ascent and descent the player wobbles a bit as if not sure whether to go up or down. This theory would also explain why once the player hits the ground, it shoots back up, because gravitational movement is canceled once the player is grounded.

 using UnityEngine;
 using System.Collections;
 
 public class JumpBlock : MonoBehaviour {
 
     private GameObject player;
 
     public float bounceFactor;
     private bool playerBounceable = false;
 
     void Awake () {
         player = GameObject.FindWithTag("Player");
     }
 
     public void bounce () {
         //Play spring animation
 
         //Bounce character upwards if player is in trigger collider
         if (playerBounceable == true) {
             player.GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * bounceFactor);
             Debug.Log("Bounce!!");
         }
     }
 
     void OnTriggerEnter (Collider other) {
         if (other.tag == "Player"){
             playerBounceable = true;
         }
     }
     void OnTriggerExit (Collider other) {
         if (other.tag == "Player"){
             playerBounceable = 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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tehnique · Jul 16, 2014 at 07:44 PM

There might be 2 possible explanations:

  • You call "bounce" from external script multiple times.

  • The player is moving so fast that OnTriggerExit is not called. This can happen sometimes on fast moving objects. Try to debug your script and see if this is the case. If so, you could check the distance to the ground after a player is bounced and set "playerBounceable" as false if he is in the air.

Comment
Add comment · Show 5 · 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 aman_jha · Jul 16, 2014 at 08:00 PM 0
Share

I know that I'm not calling it multiple times because the debug log "Bounce!!' would keep appearing, but the other two ideas are good! Since my player would move at multiple y values, do you know how I would use raycasts to detect how far from the ground I am?

avatar image Tehnique · Jul 16, 2014 at 08:07 PM 1
Share

Well, for the moment you can check if that is the case by logging "trigger enter!" and "trigger exit!". Check if there is an exit called when you bounce.

The simplest way to do detect height it is without raycasts. Just save the Y of the player when the bounce starts. In update, if playerBounceable is true check the difference between player Y and initial Y. When the difference is >x (a constant you set, that is the height to stop applying force at), set playerBounceable to false.

avatar image aman_jha · Jul 16, 2014 at 09:31 PM 0
Share

I feel so stupid for not thinking of that myself. As it turns out actually I actually had a boolean in the player physics script that checked if the player was on the ground (as you can tell I do not have a good memory) so that's ok. I'm rewriting the other parts of the jump code to make the jump dependant on the player physics and not the rigid body, because I never liked rigid bodies anyways. I'll let you know how it is in a sec

avatar image aman_jha · Jul 16, 2014 at 09:39 PM 0
Share

Ok its working! Thank you!

avatar image Tehnique · Jul 17, 2014 at 05:02 AM 0
Share

You're welcome, glad I could help!

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

23 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

Related Questions

Why wont my character jump? (using rigidbody and addForce) 1 Answer

Get result (force & torque) of AddForceAtPosition? 2 Answers

AddForce with 2D Character 2 Answers

How would I get collision at different speeds? 2 Answers

How to limit orientation of object/rigidbody? 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