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; } } }