The question is answered, right answer was accepted
Is it possible to add Angle to Raycast2D?
I'm using a raycast2D in front of the character, but I want to add some angle to it. Is it possible?
I assume, it still should go "front", not depth z-axis. :P
If You want to cast a ray from point A(ax,ay), to point B(bx,by) then just go for another way of describing the point. To be exact:
x = cos(alpha)*r
y = sin(alpha)*r,
B(cos(alpha)*r, sin(alpha)*r)
Where alpha is your angle in radians ($$anonymous$$athf.Deg2Rad), and r is distance from point A. :)
Tell me if You managed to do it, and then I will change comment to an answer, or provide further explanation and example.
Hi @Drakonno .... I don't understand how it could be from point A to point B, because o the raycast needs a direction. On my code, I'm using something like this:
hits = Physics2D.RaycastAll(transform.position, transform.localScale.x * Vector2.right, distance, layerToDetect);
This second parameter is a Vector 2 Direction. How can I do it using what you said?
Thank you for your help, and I'm sorry if I didn't get what you said...
I'm really sorry for late answer.
Let's think. What will happen, if You jusst set "direction" as Vector2(1,1)? It will shot ray from the starting point, calculate local translation (start + translation, not into global (1,1) position), and draw a ray into that direction, right? So, You can create, using provided by me equations, proper normalized vector.
I will post an proper and tested script. Feel free to copy and apply it to Your object. :)
How to get a proper hit informations is covered in many other tutorials, even from Unity devs. Go check them out for further using a rays.
Answer by Drakonno · Sep 08, 2016 at 10:09 PM
Here You have a full script:
using UnityEngine;
using System.Collections;
public class RotateRaycast_Script : MonoBehaviour
{
public Vector2 pivotPoint = Vector2.zero;
public float range = 5.0f;
public float angle = 45.0f;
private Vector2 startPoint = Vector2.zero;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
startPoint = (Vector2)transform.position + pivotPoint; // Update starting ray point.
// Direct use.
// Get normalized (of length = 1) distance vector.
// Vector2 direction = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)).normalized;
// Using function.
Vector2 direction = GetDirectionVector2D(angle);
Physics2D.Raycast(startPoint, direction, range); // Shot ray.
// Draw ray. For Debug we have to multiply our direction vector.
// Even if there is said Debug.DrawRay(start, dir), not Debug.DrawRay(start, end). Keep that in mind.
Debug.DrawRay(startPoint, direction * range, Color.red);
}
public Vector2 GetDirectionVector2D(float angle)
{
return new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)).normalized;
}
}
Cheers!
Clearer then this is impossible! Thank you man for your time and patience...
Follow this Question
Related Questions
I think my raycast detects destroyed gameobjects 0 Answers
RaycastHit2D ground detection 1 Answer
Three out of four of these raycasts aren't working 0 Answers
Racasthit2d range not working 1 Answer
Need help making a ledge climber. The problem, transform.position always returns to vector( 0, 0) 0 Answers