Make ray follow mouse position from player origin (Isometric)

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.

alt text

alt text

alt text

    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?

1 Answer

1

When a camera is at an angle to a surface, you cannot use ScreenToWorldPoint() to find that position. The solution is a Raycast(). You could Raycast() against the green block, but if the surface is flat, then there is a cheaper way. You can use Unity’s mathematical Plane class. How to setup the code will depend on how you setup your object. In particular whether the green block surface is on the XZ plane or the XY plane. Let me assume the XZ plane. To find the position it is best to use the position of the player (or whatever object you are going to place). A bit of untested example code:

 Plane plane = new Plane(Vector3.up, player.position);
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 float dist;
 plane.Raycast(ray, out dist);
 Vector3 pos = ray.GetPoint(dist);

So pos is where you would move an object in world space so that it would be under the mouse position. Note this code assumes that the Raycast() cannot fail. If you allow the camera to look parallel to the green block or away from the green block, then you need to check the return value of the plane.Raycast() call.

Another solution is to use Collider.Raycast() to cast against the block (or against an invisible plane that passes through the player’s pivot point).

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.

–

The 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?

–

![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

–

I 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?

–