Hello everyone
Its hard for me to explain what I want to reach
So I found this video where you can see what I want to do
I need to follow some object to mouse, but only in unity’s X and Z in 3D world
I tried google many times but only what I found is this
(Observe from 10 seconds)
private Vector3 screenPoint;
void Start () {
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
}
void Update () {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
transform.position = curPosition;
}
}
But this uses x y z coordiantes
Thanks for help and sorry for my terrible English.
Try this instead.
void Update () {
//do a raycast from the camera through the screen
//at the mouse cursor position into the 3d world
RaycastHit hit;
Vector3 mousePos = Input.mousePosition;
if (!Physics.Raycast(Camera.main.ScreenPointToRay(mousePos), out hit))
{
//didn't hit anything. don't do anything
return;
}
//must have hit something
//set the transform position to the point in 3d space where
//our raycast intersected something
transform.position = hit.point;
}
1 Like
With your current code, you just need to alter the y you get.
curPosition.y = transform.position.y;
transform.position = curPosition;
If you are looking for spell effects like that, you should research decals as they will be extremely helpful.
BPPHarv:
Try this instead.
void Update () {
//do a raycast from the camera through the screen
//at the mouse cursor position into the 3d world
RaycastHit hit;
Vector3 mousePos = Input.mousePosition;
if (!Physics.Raycast(Camera.main.ScreenPointToRay(mousePos), out hit))
{
//didn't hit anything. don't do anything
return;
}
//must have hit something
//set the transform position to the point in 3d space where
//our raycast intersected something
transform.position = hit.point;
}
That is what I need, thanks, it works perfectly
lordconstant:
With your current code, you just need to alter the y you get.
curPosition.y = transform.position.y;
transform.position = curPosition;
If you are looking for spell effects like that, you should research decals as they will be extremely helpful.
Thanks for your attention, I will search decals as well.