- Home /
Want to find the point straight below the controller
Hello,
In the following code I have assigned the target to be the FirstPersonController, and that much appears to work (the distance to ground thing works okay).
However, I want the magenta line to point from the controller to a point on the ground straight below it. Right now the line is going to 0,0,0 in the world itself.
Can anyone see how to make the line to straight down instead to the center of the world?
using UnityEngine;
using System.Collections;
public class CursorLocator : MonoBehaviour {
public Ray ray;
public RaycastHit hit;
public Transform target;
public Vector3 position;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//have the ray point from the player controller straight down
print (target.position.ToString ());
ray = new Ray(target.position, -target.up);
Physics.Raycast(ray, out hit);
Debug.DrawRay(target.position, new Vector3(0,0,0), Color.yellow);
Debug.DrawLine(target.position, hit.transform.position, Color.magenta);
print("Distance to ground is: " + hit.distance);
}
}
Answer by robertbu · Jul 24, 2013 at 06:46 PM
You want to use hit.point:
Debug.DrawLine(target.position, hit.point, Color.magenta);
'hit.transform.position' is the position of the object hit, not the position of the hit.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get the position of the first finger that touched the collider? 1 Answer
Why multiply a vector by -2? 1 Answer
Raycast Mouse Position To vector3 1 Answer
Moving a 3D object with 2D swipes 2 Answers
raycast and trigonometry 2 Answers