Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: Ticked_Off_Pixel on January 12, 2014, 12:18:20 PM
-
Hello everyone.
Can someone help me figuring out this problem?
I need to make a ball bounce off a wall in Unity 2D.
Before I could use the Vector3.Reflect present at the Vector3 Operator Actions.
But in Unity2D, I don't think it'll work, since there's no actions for this yet (right?)
So, does anyone knows how it can be done?
Thanks!
-
Do the object use 2D Rigidbody and 2d collider? if it is, then simply create a new 2d physics material, and set bounciness to 1 then add that material to the ball collider...
-
Hi,
I made this. Most functions seem to work but I haven't done a lot of testing.
Was easy to convert because most of the functions can implicitly cast to Vector2.
-
Hi,
If that works ok, I'll them to the action in the vector2 package.
https://hutonggames.fogbugz.com/default.asp?W1004
bye,
Jean
-
Thanks everyone.
But i'm having some problems with the workflow.
I never used Vector2 before, so what do I do?
I'm not following how this will be turned into rotation.
There is no Get Vector2 Value, only Set.
:( :( :( :(
-
Here's a bunch of Vector2 and 2D actions I've made/modified (again not all are fully tested) - hopefully you find what you need in here.
EDIT: Just realized a lot of these accomplish the same actions as the ones Jean made, but I do have a few new ones too.
-
I don't understand what you mean by turning a Vector2 into a rotation. What kind of rotation? A Vec2 is just a direction or a point in space.
-
Here's what I want to do.
(https://dl.dropboxusercontent.com/u/62133801/Vector2_doubt.png)
I don't understand which Vector2 actions i should use to:
1) get direction from arrow.
2) during collision, reflect on the normal vector of the barrier.
3) give arrow new direction.
Any ideas?
Thanks again.
-
Hi, here is my solution for your problem.
1. get direction from arrow: Handdle the direction of the ball
2. during collision, reflect on the normal vector of the barrier: obtain the surface normal for a point on a collider
3. give arrow new direction: on collision set the new direction adding force with the new vector
How to do that?
1. Create your ball, add a sprite renderer component, add a rigidbody2d component with zero gravity scale and add a Collider 2D component (not triggered)
2. Create your walls the ball will bounce on: so a gameobject with a sprite renderer and a Collider 2D component its fine.
3. The ball script:
public class Ball: MonoBehaviour
{
public float speedforce;
Vector2 direction;
Rigidbody2D rigidBody2D;
System.Random r;
void Start ()
{
//random object
r = new System.Random();
//random start direction
direction = new Vector2((float)rnd(-1.0,1.0), (float)rnd(-1.0,1.0));
//get rigidBody2D component
rigidBody2D = gameObject.GetComponent<Rigidbody2D>();
//set direction of ball adding force
setDirection(direction);
}
private void setDirection(Vector2 inDirection)
{
if(!rigidBody2D)
return;
direction = inDirection;
//Normalize directional vector
direction.Normalize();
if(speedforce>=0)
speedforce = 30;
//add force in the direction the ball bounces or starts
rigidBody2D.AddForce(direction * speedforce);
}
double rnd( double a, double b )
{
return a + r.NextDouble()*(b-a);
}
void OnCollisionEnter2D(Collision2D collision)
{
//reset force
rigidBody2D.velocity = Vector2.zero;
//obtain the surface normal for a point on a collider
Vector2 CollisionNormal = collision.contacts[0].normal;
//Reflects a vector off the plane defined by a normal.
direction = Vector3.Reflect(direction, CollisionNormal);
//apply new direction adding force
setDirection(direction);
}
}
Thats it