- Home /
How to move game object towards mouse on x and z only?
I've been trying to figure this out for weeks but what I want to do is launch a projectile towards the mouse location. The game is iso style but Im having trouble converting the mouse location to 3d space, I want the ray to be locked at the same height as the sphere when I do that though the ray no longer follows the mouse and I don't know how to fix that. Also there is an invisible plane stretching out that detects the mouse, to detect the depth in the world. Thanks for taking the time to help or read this. Code below
void Update ()
{
//when energy is in hand, holding left mouse enables to aim ball
if(Input.GetMouseButton(0) && velocity == Vector3.zero)
{
ray = Camera.main.ScreenPointToRay (new Vector3(Input.mousePosition.x, Input.mousePosition.y,
energy.transform.position.z - Camera.main.transform.position.z));
////////ray.direction = new Vector3 (ray.direction.x, Mathf.Clamp(ray.direction.y, energy.transform.position.y, energy.transform.position.y), ray.direction.z);//Clamped, but doesn't follow mouse
}
}//end of update
void FixedUpdate()
{
energy.transform.position += velocity * travelSpeed * Time.deltaTime;
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10000, mouseHitPoint))
{
Debug.Log (hit.point.x);
Debug.DrawRay (energy.transform.position, new Vector3(ray.direction.x, transform.position.y, ray.direction.z) * 200, Color.black);
}
if(releasedEnergy == true)
{
travelSpeed = 15f;
velocity = new Vector3(ray.direction.x, 0, ray.direction.z) * travelSpeed * Time.deltaTime;
energy.transform.parent = null;
}
if(destroyEnergy == true)
{
travelSpeed = 3;
velocity = new Vector3 (0, travelSpeed, 0);
}
}//end of FixedUpdate
[1]: /storage/temp/85070-example5.png
[2]: /storage/temp/85071-example4.png
Answer by IgorAherne · Jan 01, 2017 at 06:42 PM
if(Input.GetMouseButton(0) && velocity == Vector3.zero)
{
ray = Camera.main.ScreenPointToRay (new Vector3(Input.mousePosition.x, Input.mousePosition.y,
energy.transform.position.z - Camera.main.transform.position.z));
}
Don't supply any z value here. Always supply 0. You want the ray to begin from the 0th 'depth' pixel, from the pixel which is on the surface of your monitor.
Paremeters supplied into ScreenPointToRay have nothing to do with the world-coordinates.
They are in pixels, relative to your screen's lower left corner. The resulting ray however, will have its components in the world-space, so you can use them as usual world coordinates.
I believe the code should be:
void FixedUpdate()
{
//when energy is in hand, holding left mouse enables to aim ball
if(Input.GetMouseButton(0) && velocity == Vector3.zero)
{
ray = Camera.main.ScreenPointToRay (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
energy.transform.position += velocity * travelSpeed * Time.deltaTime;
Vector3 dir = Vector3.zero;
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10000, mouseHitPoint))
{
dir = new Vector3(hit.point.x, yourWantedHeightAboiveGround, hit.point.z) - energy.transform.position;
Debug.Log (hit.point.x);
Debug.DrawLine (transform.position, transform.position + dir), Color.black);
}
if(releasedEnergy == true)
{
travelSpeed = 15f;
velocity = dir.normalized * travelSpeed * Time.deltaTime;
energy.transform.parent = null;
}
if(destroyEnergy == true)
{
travelSpeed = 3;
velocity = new Vector3 (0, travelSpeed, 0);
}
}//end of FixedUpdate
Thank you so much, I spent days on this problem and got frustrated, at least I was close though. Thanks!
Your answer
Follow this Question
Related Questions
Neither CursorLockMode.Confined or SetResolution work 0 Answers
Distribute terrain in zones 3 Answers
How to translate object using script? 1 Answer
Object's localScale 1 Answer
Angry Birds Style Loader (Load the Birds to be fired) 0 Answers