Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 akyu01 · Jan 22 at 06:17 AM · ridigbody

Jumping spring (pass a rigidbody velocity value from another script)

Hello, I am writing a script for a jumping spring for a player that is supposed to make the player jump at different speeds for each spring. The issue is that I have two springs in the same scene and they always have the same speed value no matter how I change both.

the script is as following:

 public class Spring : MonoBehaviour
 {
 
     public GameObject player;
     private Rigidbody2D RB;
     public Vector2 Speed;
     private Animator anim;
     private AudioSource audi;
     private bool JumpPlayer;
     private bool JumpOnce;
 
     // Start is called before the first frame update
     void Start()
     {
         RB = player.GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>(); 
         audi = GetComponent<AudioSource>();
        
       
 
     }
     void Update()
     {
         CheckIfPushed();
         if (JumpPlayer && !JumpOnce)
         {
             StartCoroutine("PushJump");
         }
         else if (!JumpPlayer)
         {
             JumpOnce = false;
             StopCoroutine("PushJump");
             anim.SetBool("Push", false);
         }
     }
 
     // Update is called once per frame
     
    
     IEnumerator PushJump()
     {
 
         JumpOnce = true; 
         yield return new WaitForSeconds(0.1f);
         anim.SetBool("Push", true);
         RB.velocity = new Vector2 (Speed.x , Speed.y);
         audi.Play();
       
       
     }
     void CheckIfPushed()
     {
         if (GameBrain.isPushed)
         {
             JumpPlayer = true;
         }
         else if (!GameBrain.isPushed)
         {
             JumpPlayer = false;
         }
     }
     
 }
 

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 akyu01 · Jan 22 at 12:31 PM 0
Share

@knobblez yes I have two spring objects and I put two different speed values for each of them, but they react as one, when I try to jump on one or another they respond to one-speed value only, not to mention they animate the push animation together when I jump on one of them only. Actually, I prefab-ed the spring object which makes sense why they behave the same but then I tried to unpack the two of them and they still behave the same.

avatar image knobblez akyu01 · Jan 22 at 12:42 PM 0
Share

I'm assu$$anonymous$$g they share an animator, and if that's the case, then I believe since they are sharing an animator, they share variables, such as anim.SetBool("Push", true);. You can just copy the animator and assign them both. I'm not certain this is the best way to do this but it should tell you what the problem is.

And you may want to use AddForce rather than directly changing the velocity. The docs say that's not recommended as it "can result in unrealistic behaviour - use AddForce instead".

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by knobblez · Jan 22 at 10:32 AM

So you have 2 springs, each have this Spring.cs on them. You're setting the Speed Vector3 for each to different values and what happens? Are they resetting on Play or is the different just not noticeable?

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 akyu01 · Jan 23 at 07:29 AM

@knobblez it seems like the reason why they react the same is because I am using a raycast static bool (isPushed) for my player to detect if they touch the spring layer which causes them to behave the same for some reason. I tried to exchange this with player collision instead which works fine now.

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

132 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

Related Questions

Constant Force causing my object to twist/veer off. 0 Answers

(RigidBody) Velocity at an Angle 0 Answers

Help sliding Rigidbody down slope 1 Answer

How to destroy a gameObject only when inside a rigidBody 1 Answer

addForce PLEASE HELP!!! 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