I’m trying to get a firing mechanic working in my isometric game. I want to be able to shoot along the x/z-axis but not have the ray go up or down the y-axis. Instead I want it “zero’d” out on the y-axis at all times.
Also the ray doesn’t point precisely to the mouse position unless it is directly infront. As I veer left or right of the origin it keeps getting significantly less accurate at point at the mouse to the point it will be nearly 10 degrees off.
The white square denotes where the mouse cursor was. The print screen would not capture the mouse cursor for some reason.



using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
public float fireRate = 0;
public float damage = 10;
public LayerMask notToHit;
float timeToFire = 0;
Transform firePoint;
void Awake () {
firePoint = transform.FindChild ("FirePoint");
if (firePoint == null) {
Debug.LogError ("firePoint is null");
}
}
void Update () {
Vector3 mousePostion = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePostion.y = 0;
Vector3 firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y, firePoint.position.z);
float distToMouse = Vector3.Distance(mousePostion, firePointPosition) ;
RaycastHit hit;
Ray myRay = new Ray (firePointPosition, mousePostion);
Debug.DrawRay (firePointPosition, mousePostion);
if (fireRate == 0) {
if (Input.GetButtonDown("Fire1") && Physics.Raycast(myRay, out hit, distToMouse, notToHit)) {
Shoot();
}
}
}
void Shoot () {
Debug.Log ("Fire!");
}
}
How do I make a ray that stays on the same Y-axis as the player, can follow the mouse’s movement along the x/z-axis in the world with precision?
You should use the point that the gun is fired from. That may the transform.position, or that may be a spawn point that is a child of the player, or that may be the top of the cube. Whatever position you use as the starting point of your projectiles, is the point to be used.
– robertbuThe problem now is the ray does cast to the arrow but when I move the player I have my camera as the child and so what I'm thinking is that it is messing up the raycast. Basically when I move the ray actually moves as well even though I haven't moved my mouse. Do I need to make a new camera that does not move at all and point it to the ray?
– Troas![alt text][1] ![alt text][2] [1]: http://i57.tinypic.com/skypgk.png [2]: http://i58.tinypic.com/27ywfhk.png http://i57.tinypic.com/skypgk.png http://i58.tinypic.com/27ywfhk.png
– TroasI still need help figuring this out. I found out that the ray is actually pointing to the camera object and following the mouse position. I need it to point to the mouse AND follow the mouse. How do I fix this?
– Troas