Hi, I know there are a few other topics about this but I can’t find this specific issue mentioned in any other Answers or I wouldn’t be writing this.
I have a script in which I want a 3d quad game object with a texture of a target to follow the position of the mouse in 3d space across the top of a terrain.
I have this part working for the most part, the thing I can’t figure out is why the position of the target quad only matches up with the mouse pointer when the mouse is centered, otherwise there’s an offset that seems to be dependent on how far the mouse is away from the center of the camera’s viewport.
The next issue is that the position of the target quad will go whacky and create a weird feedback loop when the mouse is too close to the bottom of the screen.
It’s hard to explain, here is the script:
using UnityEngine;
using System.Collections;
public class TargetFollow : MonoBehaviour
{
public GameObject target;
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1.0f;
Vector3 newy = Vector3.zero;
Ray ray = Camera.main.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
newy = hit.point;
}
newy.y += (newy.y + 1f);
target.transform.position = newy;
}
}
If you drop this script on a game object that object you can see what I mean. When the object goes toward the bottom of the screen the Y position will start changing and I can’t figure out why. The reason I want to add the Y position of the hit point is so that the target will always be slightly above the terrain’s height.
If anyone can help I would greatly appreciate it. thanks