Diagonal movement unity 5

0

I'm a beginner in unity 5, I'm trying to create a 2d game, where I want to put diagonal moves after collisions, currently getting my object after collision is thrown up, but I can not make a diagonal move in any way, my matrix has (0,6) and (0,6) axis x (6,0) and (-6,0), I am working with 2 objects a rectangle and a circle, the circle is inside the rectangle, when the circle hits the walls of the rectangle happens the collision and action. If anyone can help me, he is grateful.

I do not have any diagonal code, just to jump up.

Code: using UnityEngine; using System.Collections;

public class Player: MonoBehaviour     {     public Vector2 jump = new Vector2 (0,300);     private Rigidbody2D rb;     private Transform tmf;

// Use this for initialization
void Start ()
    {
    rb = GetComponent<Rigidbody2D>();
   // tmf = GetComponent<Transform>();


    }

// Update is called once per frame
void Update ()
    {
    clik();
    }

//Dectando colisão de objetos e adicionando ação.
void OnCollisionEnter2D(Collision2D coll)
    {
    if (coll.gameObject.tag == "piso")
        {


            jump.y += 100;
            rb.velocity = Vector2.zero;
            rb.AddForce(jump);


    }
}



//Destruir objeto ao clickar
  void clik()
    {
    if (Input.GetMouseButtonDown(1))
        {
        Destroy(tmf);
        }
    }




}
    
asked by anonymous 03.04.2016 / 19:35

3 answers

1

You are zeroing the speed with this command: rb.velocity = Vector2.zero; this leaves no conditions to go forward or back. use rb.velocity = new Vector2 (direction * speed, rbPlayer.velocity.y); direction = 1 Go ahead direction = -1 Go backwards speed must be greater than zero (0).

    
29.03.2017 / 00:33
0

You should add a value for jump.x as well. It has a value of 0 so the object will only move in y and you want it to move in X tb.

    
05.04.2016 / 18:56
0

You can not make the move diagonally, since you are not adding values in the diagonal directions, ie in the X and Y directions together, you are only adding in Y with the jump.y += 100; stretch, what is missing is now add values also in X in this way jump.x += 100 or even

/*...*/
{
jump = new vector2(jump.x+100,jump.y+100);
rb.AddForce(jump);
}
/*...*/
    
07.03.2018 / 13:30