Set position flom RaycastHit.point
void Update () {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
transform.position = hit.point;
}
The object moves from the hit point to the origin point teleports to the hit point and moves to the camera and again and again. I want it to stay at the hit point and only move when the hit point changes.
Hello, Ivan!
If I've tested your code, the object moves exactly with the mouse pointer. Please explain your goal more detailed
I have a plane that is just set to static nothing else done to it and a standard unity cube. The goal is to move the cube along the surface of the plane. The reason I'm training to do this with rays is because i want to be able to put other geometries on top of the plane that are not part of the plane and be able to move the cube along the other geometries surface. Hope this is more clear.
Also please explain what you get with your current code
This stops the cube moving to the camera but i need to be able to move in all axes and with this i lock the Z and i tryed it with the Y and again it stops the movment to the camera but i need to move in 3d space.
Answer by Ivan Kukucov · Sep 29, 2015 at 08:33 PM
So my problem was that i had a collider on the cube that the raycast is hitting so my hit.point was moving every time its hitting the collider. Well thanks for the help and sorry for wasting you time.
Answer by hexagonius · Sep 29, 2015 at 01:31 PM
wrap the raycast with an if and change the position in there. that's the place where you know you hit something.
I asume this is what you mean and it does not work.
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
transform.position = hit.point;
}
Your answer
Follow this Question
Related Questions
ScreenToWorldPoint offset affecting ScreenPointToRay ? 1 Answer
how can my move a obect alon with the player 2 Answers
InverseTransformVector not changing frame of reference? 0 Answers
Rotate a GameObject smoothly towards mouse 1 Answer
How to make an object move forward a set distance based on its rotation? 1 Answer