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 Perdubals · Jun 04, 2017 at 05:24 AM · velocityphysics2dvector2

vector2.zero dont work in my code.

Hi, I searched the forums and couldn't find help.

I'm making a flappy bird game, using an similar structure from the tutorial.

I don't understand why when I use the vector2.zero or even if I declare the values as vector2 (0,0) the code simply ignore it.

I tried to put the vector2.zero in the "void update()" to test, but the results we're the same.

Here's the code for the scrolling object:

 public class ScrollingObject : MonoBehaviour {
 
     public static ScrollingObject instance;
 
 
     private Rigidbody2D rb2d;
     private float Speed=0;
 
 
     void Start () 
     {
         rb2d = GetComponent<Rigidbody2D> ();
         instance = this;
     }
     
     void Update () {
         if (Speed >= GameControler.instance.maxSpeed * -1) {
             Speed = Speed + (GameControler.instance.acceleration * -1);
             rb2d.velocity = new Vector2 (Speed, 0);
 
         } 
     }
      
 
     public void collision()
     {
         rb2d.velocity = Vector2.zero;
     }
 }

Thanks in advance.

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 NoseKills · Jun 04, 2017 at 10:03 AM 0
Share

Where and when do you call the method collision()?

2 Replies

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

Answer by kritoa · Jun 06, 2017 at 10:29 PM

You are setting the velocity to zero in the collision function but in the next update you set the velocity according to the old speed (plus acceleration) just like normal. It sounds like what you really want to do is set Speed=0 in the collision function, and then also maybe make a new flag (hasCollided or isDead or gameOver) and set it to true in the collision function, and in your update, check that flag before you do your acceleration.

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 Perdubals · Jun 09, 2017 at 12:25 AM 0
Share

Thanks a lot for the help, I've already tried to set the speed =0 and to use a flag, the main thing is that I was creating the flag (I supose here you are saying to create a bool) inside the ScrollingObject script, and it didn't work. When I tried to create the same flag on the GameControler script it worked as charm. Another detail is that when the speed is set to 0, the ScrollingObjects remained with the same Speed at the time the collision triggered. In the end, I didn't need the void function collision(), I just needed to set the flag on the player colision. Here's how the code is now:

On the Player Script:

     public void OnTriggerEnter2D() 
                     {
                 GameControler.instance.isDead = true;
                      }
 
 On the ScrollingObject script:

 void Update(){
                 if (GameControler.instance.isDead != true) {
                     if (Speed >= Gam
 
 eControler.instance.maxSpeed * -1) {
                     Speed = Speed + (GameControler.instance.acceleration * -1);
                     rb2d.velocity = new Vector2 (Speed, 0);
                 }
             } else {
                 
                 rb2d.velocity = Vector2.zero;
             }
              }
 
         
 Thanks again, and sorry I don't have more reputations points to give.
avatar image
1

Answer by salamander555 · Jun 04, 2017 at 07:57 AM

I think you don't really understand the voids yet. The problem is that your code just never reach the Vector2.zero line to fix your problem you need to switch the 'collision()' to "OnCollisionStay()" or "OnCollisionEnter()" and it should work

if you want I can explain yu the void function a bit more or you could look up a video about it

Comment
Add comment · Show 2 · 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 Perdubals · Jun 06, 2017 at 06:30 PM 0
Share

Thanks a lot for the answer, Indeed it has been quite some time since I took program$$anonymous$$g classes. I tried to call the function void collision() from the Player script, where I wrote :

     public void OnTriggerEnter2D()
         {
             ScrollingObject.instance.collision ();
         }
 

After read you answer, I tried to call the void function from the scrolling object script, where I tried I use all your sugestions, the way you wrote and adding 2D, tried too OnTriggerEnter2D() and OnTriggerEnter() and OnTriggerEnter2D(Collider2D other) and OnTriggerEnter2D(Collider2D Player).

Nothing seems to work.

I make a manual debug, adding some lines to show me if the void was beeing called, like this: public void collision() { rb2d.velocity = Vector2.zero; playercollision++; playercollisionText.text = "playercollision: "+ playercollision.ToString (); } And it returns every collision so far, but the background and the objects keeps accellerating and never stops.

But, when I add this lines to the void function created in the ScrollingIbject (all the ones that I mentioned above) it only show the number 1 or 2 after every collision.

And thanks for the tip, I'll look for more videos about the void function :)

avatar image kornstar83 Perdubals · Jun 09, 2017 at 01:29 AM 0
Share

try this, add a tag to the ScrollingObjects called ScrollingObject, then change your code to this,

 void OnTriggerEnter2D(Collider other)// just Collider so it will pick up ANY collider type
     {
         if (other.tag == "ScrollingObject") {//Check for the tag
             other.GetComponent<ScrollingObject>().instance.collision ();
         }
     }

This is how I always setup my OnTriggerEnter functions and they always work fine for me

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shoot Projectile on the direction of where the gun is facing 1 Answer

Stop object velocity from moving horizontally 1 Answer

i am not able to increase the speed of ball perodically in pong, in this code the speed of ball decrease while playing so help with this, how to increase speed of ball perodically?, 1 Answer

Character won't move (Fixed) 1 Answer

Android velocity slower than editor 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