- Home /
bug with Raycast?
I want to let a Raycast between two characters, but it's going in the wrong direction (ray #2)
And DrawLine with the same settings as the Raycast is in the right direction (ray #1)
//#1
Debug.DrawLine(this.transform.Find("Target").transform.position, i.transform.Find("Target").transform.position, Color.red);
//#2
if (Physics.Raycast(this.transform.Find("Target").transform.position , i.transform.Find("Target").transform.position, out hit , dist))
{
Debug.DrawLine(this.transform.Find("Target").transform.position, hit.point, Color.red);
if (hit.transform.tag == tagE)
{
hit.transform.GetComponent<Helth>().CurrentHP -= 1;
}
}
What's wrong?
For debugging raycast you might also look at the [Debug.DrawRay] : https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
Answer by Ambrose998800 · Aug 01, 2018 at 10:24 PM
Raycast and Drawline have two different casting systems: raycast works with a vector for the ray direction (from -> into direction), drawline works with start and end point (from -> to). You will need to calculate the direction vector by position difference (Character2.transform.position - Character1.transform.position).
Your answer
![](https://koobas.hobune.stream/wayback/20220612172017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding script during runtime 2 Answers
How to detect if two objects become a square ? 1 Answer
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers
Script is giving error that no MonoBehaviour scripts in file 0 Answers
How to dynamically construct a reference to a game object by name 2 Answers