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 /
avatar image
0
Question by FlashX · Mar 29, 2017 at 04:46 AM · addforcespring

Character falls through spring (and floor sometimes)

Hi guys,

could anyone help me out here?

When my character touches a spring object, he flies up into the air as expected, but when he comes back down again, he falls through the spring and does nothing, i then have to get my character to walk outside the object then back in again to activate it. what am i doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class Spring : MonoBehaviour {
 
 public GameObject player;
 private Rigidbody RbPlayer;
 public int springAmount;
 //private bool springHit = false;
 
     // Use this for initialization
     void Start () {
         RbPlayer = player.GetComponent<Rigidbody>();
     }
     
 
     void OnTriggerEnter(Collider other)
  {
      if (other.tag == "Spring")
      {
         // springHit = true;
 
          RbPlayer.AddForce (new Vector3 (0, springAmount,0)); 
 
      }
  }
 
 
 }
 
Comment
Add comment · Show 1
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 Rhombuster · Mar 29, 2017 at 05:47 AM 0
Share

I'm unable to submit answers for some reason. When your character is falling, you need more force to counteract it. That's why it works when you walk in (your y velocity is probably 0) unlike when you fall in (your y velocity is a larger negative number).

4 Replies

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

Answer by Kossuranta · Mar 29, 2017 at 07:27 AM

Like Rhombuster said, while falling the AddForce will only slow you down. Try this to reset your falling velocity just before adding force:

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Spring")
     {
     // springHit = true;
     Vector3 tempVelocity = RbPlayer.velocity;
     tempVelocity.y = 0;
     RbPlayer.velocity = tempVelocity;
     RbPlayer.AddForce (new Vector3 (0, springAmount,0)); 
     }
 }



Or if the object on spring will always have same mass then you could just set y velocity instead of using AddForce.

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Spring")
     {
     // springHit = true;

     RbPlayer.velocity = new Vector3 (RbPlayer.velocity.x, springAmount, RbPlayer.velocity.z); 
     }
 }
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
avatar image
0

Answer by Rhombuster · Mar 30, 2017 at 04:30 PM

In order for AddForce to work, it needs to counteract the force of you coming down. This worked for me, you'll probably have to tone down the amount of force you apply.

     void OnTriggerEnter(Collider other) {
         Debug.Log("JSDFJSDF");
         if (other.tag == "Spring") {
             // springHit = true;
 
             RbPlayer.AddForce(new Vector3(0, springAmount + Mathf.Abs(RbPlayer.velocity.y), 0), ForceMode.VelocityChange);
 
         }
     }
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
avatar image
0

Answer by Rhombuster · Mar 30, 2017 at 04:30 PM

Your builds up velocity when he is falling. It works the first time, because you have no downward velocity. So you need to counteract the -y velocity. I tried to post this before but it got deleted.

 RbPlayer.AddForce(new Vector3(0, springAmount + Mathf.Abs(RbPlayer.velocity.y), 0), ForceMode.VelocityChange);

You will need to reduce your spring amount. Let me know if you need more help.

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

Answer by FlashX · Mar 29, 2017 at 07:32 AM

OMG! you guys are great! thank you so much for explaining that and helping me out with he code, very much appreciated! :)

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

66 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

Related Questions

Adding a force to an object relative to a rotation? 0 Answers

Moving player to the opposite direction of the mouse position 1 Answer

Mistake in Function with Collision and AddForce 0 Answers

2D Rotate towards GameObject acting odd. 2 Answers

Player Follow Function 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