I’ve never liked Raycasts but this one really has me stumped. Here’s a picture: (red is raycasts, green is where I clicked the mouse)

Here’s my code: (2D Raycast at line 121
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour {
public Rigidbody2D rb;
public Animator anim;
public Transform spriteRoot;
public Transform firingPoint;
public Transform aimAssist;
public GameObject bulletImpact;
public LayerMask BulletRayMask;
public Camera mainCamera;
public float maxSpeed;
public float speedAccel;
public bool facingLeft;
public bool facingRight;
public bool sliding;
private Vector3 mousePos;
public GameObject debugCube;
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
//For Input
void Update() {
mousePos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
//----SHOOTING----//
if (Input.GetMouseButtonDown (0)) {
ShootBullet (new Vector2(firingPoint.position.x, firingPoint.position.y), new Vector2(mousePos.x, mousePos.y));
}
//----END----//
}
//For Physics
void FixedUpdate () {
//----AIMING----//
if (mousePos.x < transform.position.x && !sliding) {
facingLeft = true;
facingRight = false;
} else if (mousePos.x > transform.position.x && !sliding) {
facingLeft = false;
facingRight = true;
}
if (facingLeft) {
spriteRoot.localScale = new Vector3(-5, 5, 5);
aimAssist.rotation = Quaternion.LookRotation (Vector3.forward, mousePos - transform.position);
} else if (facingRight) {
spriteRoot.localScale = new Vector3(5, 5, 5);
aimAssist.rotation = Quaternion.LookRotation (-Vector3.forward, mousePos - transform.position);
}
anim.Play ("Aim", 1, (1f/180f)*(aimAssist.eulerAngles.z));
//----END----//
//----MOVEMENT----//
if (Input.GetKey (KeyCode.LeftShift)) {
sliding = true;
anim.SetBool ("Run", false);
anim.SetBool ("BackwardsRun", false);
anim.SetBool ("BackSlide", true);
} else {
sliding = false;
anim.SetBool ("BackSlide", false);
}
if (Input.GetKey (KeyCode.A) && !sliding) {
if (facingLeft) {
anim.SetBool ("Run", true);
anim.SetBool ("BackwardsRun", false);
} else if (facingRight) {
anim.SetBool ("Run", false);
anim.SetBool ("BackwardsRun", true);
}
if (rb.velocity.x > -maxSpeed)
rb.AddForce (new Vector2 (-speedAccel, 0));
} else if (Input.GetKey (KeyCode.D) && !sliding) {
if (facingLeft) {
anim.SetBool ("Run", false);
anim.SetBool ("BackwardsRun", true);
} else if (facingRight) {
anim.SetBool ("Run", true);
anim.SetBool ("BackwardsRun", false);
}
if (rb.velocity.x < maxSpeed)
rb.AddForce (new Vector2 (speedAccel, 0));
}
if (!Input.GetKey (KeyCode.A) && !Input.GetKey (KeyCode.D)) {
anim.SetBool ("Run", false);
anim.SetBool ("BackwardsRun", false);
}
//----END----//
}
void ShootBullet(Vector2 spawnPos, Vector2 directionPos) {
RaycastHit2D hit = Physics2D.Raycast (spawnPos, directionPos, 1000f, BulletRayMask);
if (hit.collider != null) {
GameObject bulletImpactObj = GameObject.Instantiate (bulletImpact, new Vector3 (hit.point.x, hit.point.y, 0), Quaternion.identity);
}
Debug.DrawLine (new Vector3(spawnPos.x, spawnPos.y, 0), new Vector3(directionPos.x, directionPos.y, 0), Color.green, 5f, false);
Debug.DrawLine (new Vector3(spawnPos.x, spawnPos.y, 0), new Vector3(hit.point.x, hit.point.y, 0), Color.red, 5f, false);
GameObject.Instantiate (debugCube, new Vector3(directionPos.x, directionPos.y, 0), Quaternion.identity);
anim.SetTrigger ("GunKickback");
}
}
I originally thought the mouse coords were inaccurate. The debug lines, however, show that the mouse positions are correct, but for whatever reason, setting off a Raycast in the direction of the mouse is horribly inaccurate. I can’t wrap my head around why. Can someone with a better understanding of Raycasts explain what in the hell is going on? Thanks.