Hey, I am making a game and i am having trouble with this area.
The problem is I want to record where my mouse is on the X, Y, Z location when I press down and i am hoping i could store this as a variable or 3 variables because then i could use that code to tell an object to go to that location. I do not know where to start, i read another post similar to mine and that was about ray cast but that just confused to much, but when i got it to work i put the script on the floor and nothing happening i put it on my object and it spun out and jumped towards the camera, this was the code i prevoisly tried:
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
var hitPoint = hit.point;
}
}
some extra details:
my object is a cube with box collision
my floor is a large cube
the code i will use to tell the object to go to the next location would be a way point code which i found on YouTube, i am just hoping i could use the X, Y, Z locations to put them onto the GUI for the object to follow.
Your code looks perfectly fine. You may want to check your Physics settings inside your scene and the objects that you’re working with.
When you look at a GameObject, the center of the object, in your case a cube, is well, in the center. So when you think about it, the hit.point that you’re having the cube move to on the floor is trying to move it’s center point to the hit.point on the floor. So, if you have gravity active in the scene and your objects are not kinematic, that is going to explain why your cube “spun out and jumped towards the camera.”
You either have to A:
Create an offset on your Y position ( assuming Y is still your world up axis ) to compensate for the two box colliders overlapping each other.
Or B:
Make a simple primitive model that has it’s pivot changed inside a 3D Modeling program and import it to compensate for this overlap as well. The reason why I say this is because the norm for the pivot point of most characters in a 3D game is at their feet to better symbolize where you want them to move and what their position is in relation to the ground that they’re standing on. This also fixes the issue that you might be having.
What you may want to try is also setting the two objects to ignore collision on each other and set them both as kinematic on your Rigidbodies. If you don’t have rigidbodies on your objects, this may explain why you’re having spinning out of control problems as well.
Here you go, put this script in your scene, attach the cube that you want to move to the script’s “Move Me” variable in the editor. This should give you a starting point to work from.
var moveMe : GameObject;
function Update()
{
//Check for the mouse being pressed (also works as a touch input)
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
moveMe.transform.position = hit.point;
}
}
}