- Home /
How to set position of raycast higher?
So I have this code snipped to draw a ray:
RaycastHit hit;
Ray forward = new Ray(transform.position, transform.forward);//transform.TransformDirection(Vector3.forward) * 40;
Debug.DrawRay(transform.position, transform.forward*40, Color.green);
if(Physics.Raycast(forward, out hit, 70) && hit.collider.tag == "player")
{
AddReward(0.001f / 100000);
hitDistanceGlobal = hit.distance;
//Debug.Log("hitting Player");
}
This ray is attached to an agent and the issue is that I want the position of the ray higher. If I use this code the ray is at the feet. At the deepest point of the agent. How can I change this?
Comment
Answer by Eno-Khaon · Feb 17, 2019 at 10:01 AM
When you create the Ray, you're providing it with a position (transform.position) and a direction (transform.forward).
You can freely define both of these attributes of the Ray. As an example in your case:
// Apply a raw offset
Vector3 offset = new Vector3(0.0f, 1.0f, 0.0f);
Ray forward = new Ray(transform.position + offset, transform.forward);
// Attach an empty GameObject as a child to the agent and use its location
public Transform rayPos;
// ...
Ray forward = new Ray(rayPos.position, transform.forward); // or rayPos.forward
Your answer
![](https://koobas.hobune.stream/wayback/20220612191844im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
why are child colliders sometimes ignored? 2 Answers
Click&Drag Misterious Disappearing! 1 Answer
Don't allow raycasts to go through colliders? 3 Answers
I don't know why i can't detect by ray sth. tagged, 1 Answer
Rays: why is not working? 1 Answer