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 11, 2014 at 11:02 AM · bugvelocityrecordingvertical

rigidbody2D.velocity.y not recording C#

Hello ^_^

setup - vertical oblong & horizontal oblong, both have this script attached. a circle tagged as "projectile" is fired at the oblong (using AddForce). the script is supposed to record the x & y velocity of the ball when it hits.

for the vertical oblong this works fine.

for the horizontal oblong this only works on the short edges.... on the long edges only the x velocity is recorded. The ball is very definitely moving in the y when it hits.

does anyone have any idea why this doesn't work on the long sides of the horizontal oblong?

here is the script..... I had other stuff going on, but have commented out the other stuff to narrow down what's not working. I have put the projectile gameobject into a variable incase that was the problem rather than just reading the coll.gameObject.rigidbody2D.velocity for x & y. hasn't made any difference:

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


can anybody please tell me why this doesn't work?.... or have I found a bug?

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 Raimi · Aug 11, 2014 at 03:44 PM

I think this might help...

     public Vector2 velocity;
     
     void OnCollisionEnter2D (Collision2D col)
     {
         if (col.gameObject.tag == "Projectile")
         {
             velocity.x = col.gameObject.GetComponent<Rigidbody2D>().velocity.x;
             velocity.y = col.gameObject.GetComponent<Rigidbody2D>().velocity.y;
 
             Destroy (this.gameObject);
         }
     }

EDIT: SOLUTION

Put this inside your PROJECTILE instead of TARGET...

     public Vector2 velocity;
 
     void OnCollisionEnter2D (Collision2D col)
     {
         if (col.gameObject.tag == "Target")
         {
             velocity.x = gameObject.GetComponent<Rigidbody2D>().velocity.x;
             velocity.y = gameObject.GetComponent<Rigidbody2D>().velocity.y;
 
             Destroy (col.gameObject);
         }
     }

I tested this and it works :)

Comment
Add comment · Show 4 · 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 11, 2014 at 04:02 PM 0
Share

Thankyou ^_^

that still didn't recognise a y on the long side of the horizontal oblong..... interestingly it also didn't record a y on the short side of the vertical either.....

so none of the horizontal edges are now recording a y....... confusing, but at least it's consistent.

avatar image Raimi · Aug 11, 2014 at 04:40 PM 0
Share

Just wanted to add that the vector2 velocity should be on another script or it will be removed with object.

avatar image Raimi · Aug 11, 2014 at 04:56 PM 0
Share

Just added solution to answer

avatar image KittenPoker · Aug 11, 2014 at 10:59 PM 0
Share

Thankyou ^_^

I re-wrote the script so it works from the projectile ins$$anonymous$$d of the oblong. It all works fine from that direction (script on the projectile, no script on the oblong).

I still thing there may be a bug, but at least there is a work around ^_^

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

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

[bug] Velocity set to 0 by Unity 1 Answer

,Problems with capsule collider 2d and velocity 0 Answers

Input.GetAxisRaw isnt being called? 1 Answer

Clones are being downscaled for no reason? 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