Physics2d.Raycast always returning 0,0
Hello I am trying to make a 2d gun object using a raycast to create bullet trails. The issue is the raycast appears to be always returning 0, 0 any help would be appreciated. using System.Collections; using System.Collections.Generic; using UnityEngine; public class backwardsPistol : MonoBehaviour { public GameObject player; public GameObject weapon; public float bulletTrailTime = 0.1f; public float wepMirror = 1; public float range; public float fireRate; public float wepRotate = 0; public float adjacentW = 0; public float oppositeW = 0; public Vector2 offset = new Vector2(0,1); public Vector2 playerLocation; public Vector2 weaponLocation; public Vector3 mousePos; public Vector3 hitPos; RaycastHit2D hit; // Use this for initialization void Start () { } // Update is called once per frame void FixedUpdate () { //Get the mouse pos on a 2d plane mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Get player and weapon positions then set the weapon position playerLocation = player.transform.position; weaponLocation = weapon.transform.position; weapon.transform.position = playerLocation + offset; //get opposite and adjacent and do tan calc oppositeW = mousePos.y - weaponLocation.y; adjacentW = mousePos.x - weaponLocation.x; wepRotate = Mathf.Atan2(oppositeW, adjacentW); wepRotate = wepRotate * Mathf.Rad2Deg; //Check if aiming. If not set facing direction based on player move direction if (Input.GetMouseButton(1)) { if (mousePos.x < player.transform.position.x) { weapon.transform.localRotation = Quaternion.Euler(0, 0, wepRotate); Debug.Log("Aiming"); wepMirror = -1; } else { weapon.transform.localRotation = Quaternion.Euler(0, 0, wepRotate); wepMirror = 1; } } else if (Input.GetKey("d") && !Input.GetKey("a")) { weapon.transform.localRotation = Quaternion.Euler(0, 0, 0); wepMirror = 1; } else if (Input.GetKey("a") && !Input.GetKey("d")) { weapon.transform.localRotation = Quaternion.Euler(0, 180, 0); wepMirror = -1; }else if ((!Input.GetKey("a") && !Input.GetKey("d")) || (Input.GetKey("a") && Input.GetKey("d"))) { } //Fire weapon if (Input.GetMouseButtonDown(0)) { hit = Physics2D.Raycast(weapon.transform.position, Vector2.right, 100, 9); hitPos = hit.point; Debug.DrawLine(weapon.transform.position, hit.point, Color.gray, bulletTrailTime); } } }
Your answer
Follow this Question
Related Questions
Having an issue Making a second ray cast with a new angle 1 Answer
raycast : get normal value of linecast hit 1 Answer
How can I make a third person camera collision script? 2 Answers
Raycast doesn't hit instantiated object within same frame of instantiation. 0 Answers
Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers