playMaker

Author Topic: Vector Reflect in a Unity 2D (4.3) game  (Read 15995 times)

Ticked_Off_Pixel

  • Playmaker Newbie
  • *
  • Posts: 31
    • http://happystar-adventures.tumblr.com/
Vector Reflect in a Unity 2D (4.3) game
« 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!

cloud03

  • Full Member
  • ***
  • Posts: 249
  • Viva Playmaker!
    • Milkish Game Studios Blog
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #1 on: January 12, 2014, 10:01:12 PM »
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...

dasbin

  • Junior Playmaker
  • **
  • Posts: 92
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #2 on: January 12, 2014, 11:07:45 PM »
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.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #3 on: January 13, 2014, 06:32:08 AM »
Hi,

If that works ok, I'll them to the action in the vector2 package.

https://hutonggames.fogbugz.com/default.asp?W1004

 bye,

 Jean

Ticked_Off_Pixel

  • Playmaker Newbie
  • *
  • Posts: 31
    • http://happystar-adventures.tumblr.com/
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #4 on: January 13, 2014, 09:36:06 AM »
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.

 :( :( :( :(

dasbin

  • Junior Playmaker
  • **
  • Posts: 92
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #5 on: January 13, 2014, 05:50:56 PM »
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.
« Last Edit: January 13, 2014, 05:52:40 PM by dasbin »

dasbin

  • Junior Playmaker
  • **
  • Posts: 92
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #6 on: January 13, 2014, 05:53:48 PM »
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.

Ticked_Off_Pixel

  • Playmaker Newbie
  • *
  • Posts: 31
    • http://happystar-adventures.tumblr.com/
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #7 on: January 14, 2014, 07:22:52 AM »
Here's what I want to do.



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.

ikarusfr

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Vector Reflect in a Unity 2D (4.3) game
« Reply #8 on: January 19, 2014, 04:15:58 PM »
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