Search Unity

Stop when sprite collider whit board limit [Collider2D]

Discussion in '2D' started by Middrel, Apr 22, 2017.

  1. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Hi,

    I have a question about stop my player when it collider with board limits. I have been seeing some guides and tutorials and apparently this is the best option, but I do not get it to work.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D coll)
    2.     {
    3.         if (coll.gameObject.name == "wall")
    4.         {
    5.             Debug.Log("Colision");
    6.             r2bd.velocity = Vector3.zero;
    7.         }
    8.     }
    In my case, I have, in this moment, 3 box colliders. In background there are one to left and one to right. In middle of board there are one collider more. The sprite only should be move in your own side board but it try to cross to other side. Don't stop completely when colliding.

    You can see the stats to my objects in the images attached in this post or in the urls bellow:

    http://www.subirimagenes.com/imagen-background-9726960.html
    http://www.subirimagenes.com/privadas-ayer-2403436.html
    http://www.subirimagenes.com/privadas-wall-2403438.html

    Regards!!
     

    Attached Files:

  2. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Ok, I change r2bd.velocity = Vector3.zero by r2bd.velocity = Vector2.zero but the problem persists. Yo can see what happens in the next video:



    Do you know what I'm doing wrong?

    Regards!
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Using Vector3 or Vector2 makes no difference as Unity converts Vector3 to Vector2 by dropping the Z but you should try to be more precise and use types that methods and properties expect. For 2D physics that's Vector2.

    Are you not aware that there are two velocities at work in physics; linear velocity and angular velocity? The video shows the player block rotating and you do not mention resetting the angular velocity. You should set that to zero as well.

    I can only presume that "r2bd" it stands for "rigid two body dimension" which makes no sense! If that's from a Unity tutorial then the person who wrote it needs a slap on the wrist. :)
     
  4. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Hi Melv, thanks for your answer...

    I try it with: r2bd.angularVelocity = Vector2.zero;

    But I get a error: Can not implicitly convert type 'UnityEngine.Vector2' to float

    Maybe my error is in Unity and not in code?

    Regards!
     
  5. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Ok, maybe it's not a best practice... if I set speed to 0, my sprite stop. If my sprite only move one time by turn maybe is correct, but I think it's not the right way... What do you think?

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3.     public float speed;
    4.     private Vector3 target;
    5.     private Vector3 target_rot;
    6.  
    7.     private bool sprite;
    8.     public Rigidbody2D r2bd;
    9.  
    10.     public int turn = 0;
    11.    
    12.     void Start()
    13.     {
    14.         target = transform.position;
    15.         r2bd = GetComponent<Rigidbody2D>();
    16.  
    17.     }
    18.    
    19.     void Update()
    20.     {
    21.         sprite = true;
    22.  
    23.         if (Input.GetMouseButtonDown(0))
    24.         {
    25.             if (turn == 0) {
    26.                 if (sprite)
    27.                 {
    28.                     target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    29.                     target.z = transform.position.z;
    30.                     target_rot = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    31.                 }
    32.             }
    33.             turn = 1;
    34.         }
    35.         transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    36.         var angle = Mathf.Atan2(target_rot.y - transform.position.y, target_rot.x - transform.position.x) * Mathf.Rad2Deg;
    37.         transform.rotation = Quaternion.Euler(0, 0, angle + 90);      
    38.     }
    39.  
    40.     void OnCollisionEnter2D(Collision2D coll)
    41.     {
    42.         if (coll.gameObject.tag == "wall")
    43.         {
    44.             speed = 0;
    45.             r2bd.velocity = Vector2.zero;
    46.             r2bd.angularVelocity = 0;
    47.         }
    48.  
    49.         if (coll.gameObject.tag == "enemy")
    50.         {
    51.             speed = 0;
    52.             r2bd.velocity = Vector2.zero;
    53.             r2bd.angularVelocity = 0;
    54.         }
    55.     }
    56.  
    57.  
    58. }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Sounds like you're just guessing and not looking at the documentation. Rigidbody2D.angularVelocity is a float not a Vector2.

    Why on earth are you modifying the Rigidbody2D velocity when you're completely ignoring it by stomping over the position/rotation of the Rigidbody2D each frame by modifying the Transform.

    You add a Rigidbody2D because you want IT to modify the Transform and you should then not touch it yourself, especially not per-frame. If the Rigidbody2D has velocity then it'll update the Transform position/rotation, not you.
     
  7. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    More than guessing I'm exploring what I have and what I find to know that I do everything.

    True, I do not yet have the ability to develop a game and there is information that we should, I think, ask and consult with more experienced people. But everything step by step.

    And in fact, it is not the same to turn a rectangle than to turn a sphere as in the tutorials. So we ask a little more specific things.

    I will investigate more about it and come back if I have something more concrete.

    Regards! and thanks for your answer!!