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 KittenPoker · Aug 09, 2014 at 08:08 PM · velocityifnotverticalhorizontal

Don't know why checking if not something stops the whole thing working

Hello ^_^

context.... 2D.... script is on a block, currently on a Z rotation of 90 (vertical). The projectile hits it (it's recording x & y velocities from the projectile around 6-7 each on test), the block is destroyed & the projectile disappears. This all works fine if I don't check for !vert, ie if the block is horizontal, which it could be, but isn't at the moment.
The idea being that the projectile needs to have some sort of velocity heading towards the block to destroy it.

here is the code:

 public class Breakable : MonoBehaviour {
 
     public bool vert;
     public float xVel;
     public float yVel;
 
     void Start ()
     {
         if (this.transform.rotation.eulerAngles.z <45f)
             vert = false;
         else
             vert = true;
     }
 
 
     void OnCollisionEnter2D (Collision2D coll)
     {
         if (coll.gameObject.tag == "Projectile")
             xVel = coll.gameObject.rigidbody2D.velocity.x;
             yVel = coll.gameObject.rigidbody2D.velocity.y;
 
             if (vert && xVel > 1f)
                 {
                     coll.gameObject.SetActive (false);
                     Destroy (this.gameObject);
                 }
             
             else if (!vert && yVel > 1f)
                 {
                     coll.gameObject.SetActive (false);
                     Destroy (this.gameObject);
                 }
                 
     }
 }


it works if I don't check vertical at all & it works if I check it is vertical both times, but it doesn't work if I check it is not vertical anywhere....... any idea why & how I can fix it?

Stuff is public just so I can see it while in play.

Further to the above....... I have tried changing one of the blocks with this script on to a z rotation of 0, so that it counts as horizontal (!vert). I took out the !vert from the else.

The script will destroy the vertical block, but does not record a yVel on the horizontal block......but checking the projectile in play it does have quite a bit of y movement at the time it hits. So now I am more confused.

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
0

Answer by Bunny83 · Aug 10, 2014 at 08:09 AM

I'm not sure what you actually want to do, but let me fix your indention and you might see your problem:

 void OnCollisionEnter2D (Collision2D coll)
 {
     if (coll.gameObject.tag == "Projectile")
         xVel = coll.gameObject.rigidbody2D.velocity.x;
 
     yVel = coll.gameObject.rigidbody2D.velocity.y;
 
     if (vert && xVel > 1f)
     {
         coll.gameObject.SetActive (false);
         Destroy (this.gameObject);
     }
     else if (!vert && yVel > 1f)
     {
         coll.gameObject.SetActive (false);
         Destroy (this.gameObject);
     }
 }

As you can see the first "tag"-condition only covers the xVel assignment. You probably want to enclose the whole thing in brackets {}

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 KittenPoker · Aug 10, 2014 at 08:54 AM 0
Share

good call on the missing brackets.... thankyou ^_^

Doesn't seem to have changed anything though.... still only records a yVel if the block is vertical & only destroys the vertical block is we don't check for !vert.

BTW.. the idea is that we don't want the projectile to destroy the block if it just rolls across it, it needs to actually hit it.... which is why if it's a vertical block the projectile needs to have some x velocity & if its a horizontal bock the projectile needs some y velocity.

Thanks ^_^

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

22 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

Related Questions

Ball stuck horizontally between rackets in pong 0 Answers

Get walk animation to play on horizontal and vertical axis presses. 2 Answers

How to detect Double Clicking Horizontal/Vertical Axis on joystick 0 Answers

How to control "Horizontal" and "Vertical" axis using UI Buttons 0 Answers

AddForce on GetAxis always sam direction? 1 Answer


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