- Home /
How to Translate Object to Mouse Click Point in Game-World Coordinates
Hey everyone, I swear I have gone through every link, forum, and tutorial on how to get this to work, yet nothing works. Thank you for taking the time to read this, my first post - my frustration is killing me. As simple as people are saying this is, I beg you not to merely link me to another example, I've seen at least 10 and I've spent 2.5 hours on it.
Goal: I want a cube (call it A) to translate from its current point to a mouseclick point. Since I read that in space a mouse point would be ambiguous, I created a second object (call it B) to click on, for reference.
What I Tried: As seen below, I tried using answers from forums. To see what coordinates were being returned when I clicked on B, I added a debug lines marked below - yet no matter where I clicked the coords returned were always the same.
What Actually Happens: Cube A moves in a completely unexpected direction, no where near the click area
Possible Reasons for Not Working: 1. hitPos is always the same value regardless of where I click 2. hitPos returns (-0.9, 3.7) on cube B positioned at (2.7, 0) - doesn't make sense function OnMouseDown () {
var hit : RaycastHit;
var hitPos : Vector3 = Camera.main.ScreenToWorldPoint(hit.point);
var theCube = GameObject.Find("Cube");
var startPos : Vector3 = theCube.transform.position;
var endPos : Vector3 = hitPos;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit))
{
var index =0.0;
var rate = 0.2;
//debug commands to print off coordinate values
Debug.Log(startPos);
Debug.Log(endPos);
Debug.Log("Impact at: "+hitPos);
while( index < 1.0 ) { theCube.transform.position = Vector3.Lerp( startPos, endPos, index ); Debug.DrawRay(startPos, endPos, Color.red); //goes nowhere near click area index += rate * Time.deltaTime; Debug.Log(theCube.transform.position); //another debug to track Cube A's coords if (startPos == endPos){ //I realize this if statement is probably pointless Debug.Log("Done!"); break; } yield; }
}
}
Answer by Hyperion · Sep 01, 2013 at 04:36 PM
So you want the object to move to mouse.
//attach this to your camera
static var mousepos : Vector3;
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit,1000))
{
mousepos = hit.point;
}
}
//attach this to your object
if (Input.GetMouseButtonDown (0)){
transform.position = mouse.mousepos;
}
This is a basic example. Hope it helps.
Yes! This code lead me on the right path to getting what I needed. With a few tweaks it was perfect. I am incredibly grateful for your help Hyperion, thank you very much.
What Happens Now: Cube A appears where I click
The only remaining issue now is how to incorporate movement speed, which I tried to achieve in the code I posted earlier. I tried multiplying mousepos by Time.deltaTime, and inserting that result as a parameter of transform.Translate. However it moved in an unexpected direction. From my Debug.Log line (prints the coords of mouseclick, the value of Time.deltaTime, and the destination coord defined as mousepos*Time.deltaTime:
Area to send it to: (3.4, 0.0, 7.1) Time unit: 0.01656607 Destination: (0.1, 0.0, 0.1) Clearly the destination point is no longer the clicked point.
Thanks again Hyperion for your help. If you're able to help me further I'll certainly appreciate it. Anybody that knows how to incorporate movement speed, I will be grateful for your help.
Bonus Goal: One of the tweaks to my code was that I changed cube B to a terrain object, so that when I click somewhere in the terrain cube A moves to the clicked area. Assu$$anonymous$$g cube A could translate to a click spot with a movement speed, would the code work for clicking on a sloped part of terrain (say a hill)? Or would I do something like figure out at what point in the desired direction does the hill start, translate to that point, then translate up the hill?
You're welcome. Could you please accept my answer? As for the 'appearance', try using lerping. You can find out more about that at the link: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
transform.position = Vector3.Lerp(cube.position, mouse.mousepos, 1);
Or something like that...
Awesome, thank you. I modified Lerp to $$anonymous$$oveTowards, and it works great. You're the best! I owe you a beer.