- Home /
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?
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 :)
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.
Just wanted to add that the vector2 velocity should be on another script or it will be removed with object.
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