RayCast Error?

Hi i’m having some problems with my raycast.
When i shoot it doesn’t go straight?!

My script is here:

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

	public float fireRate = 0;
	public float Damage = 10;
	public LayerMask whatToHit;
	
	public Transform BulletTrailPrefab;
	public Transform MuzzleFlashPrefab;
	float timeToSpawnEffect = 0;
	public float effectSpawnRate = 10;
	
	float timeToFire = 0;
	Transform firePoint;

	// Use this for initialization
	void Awake () {
		firePoint = transform.FindChild ("FirePoint");
		if (firePoint == null) {
			Debug.LogError ("No firePoint? WHAT?!");
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (fireRate == 0) {
			if (Input.GetButtonDown ("Fire1")) {
				Shoot();
			}
		}
		else {
			if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
				timeToFire = Time.time + 1/fireRate;
				Shoot();
			}
		}
	}
	
	void Shoot () {
		Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
		Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
		RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
		if (Time.time >= timeToSpawnEffect) {
			Effect ();
			timeToSpawnEffect = Time.time + 1/effectSpawnRate;
		}
		Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
		if (hit.collider != null) {
			Debug.DrawLine (firePointPosition, hit.point, Color.red);
			Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
		}
	}
	
	void Effect () {
		Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
		Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
		clone.parent = firePoint;
		float size = Random.Range (0.6f, 0.9f);
		clone.localScale = new Vector3 (size, size, size);
		Destroy (clone.gameObject, 0.02f);
	}
}

Image:
Blue is raycast
Orange is the effect
i need the raycast to line up with the orange fx

Hi @w212ds

First

  • You are using Raycast in line 44, where first paramerter is origin point and 2nd point is direction (Unity - Scripting API: Physics2D.Raycast) here second parameter ( mousePosition-firePointPosition ) become direction which are correct
  • And another hand you draw line with cyan color in line 49 using Debug.DrawLine where first parameter is same as raycast, its origin point. But, but 2nd parameter is destination point, not direction so your both value always get diffrent. in this case ( mousePosition-firePointPosition ) become destination point which are wrong. (Unity - Scripting API: Debug.DrawLine)
  • In line number 50 - 53 you are checking hit info and if hit info find then it will draw another line with red color which are correct. where in that line you use exact point to destination.

Second

may be you also need to chack roataion of GameObject / prefeb which you used for “MuzzleFlashPrefab” and child objects rotation. there are chanse to something wrong there.