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