26 de mai. de 2017

Converting Angle to Vector direction




If the reflected angle (green) is greater than 50º, I want to change its direction for an angle of 45º (orange).


public Vector2 direction = new Vector2(1,1);

void FixedUpdate () {
 rb.velocity = direction * velocity;
}

private void OnCollisionEnter2D(Collision2D collision)
{
 Vector2 normal = collision.contacts[0].normal;
 float angle = Vector2.Angle(direction, normal));
 float angleComp = 180 - angle;
 
 Vector2 newDirection =  Vector2.Reflect(direction, normal);
 newDirection.Normalize();
 
 if (angleComp > 50)
 {
  newDirection = Quaternion.AngleAxis(direction.x * 45, Vector3.forward) * normal;
  newDirection.Normalize();
 }
 
 direction = newDirection;
}

Nenhum comentário: