Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Raycasting issues

Discussion in '2D' started by Zurvivor0412, Dec 6, 2016.

  1. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    As well the raycast(debug.drawline) is going the opposite way to the mouse pointer location.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Weapon : MonoBehaviour {
    6.  
    7.     public float fireRate = 0;
    8.     public float Damage = 10;
    9.     public LayerMask whatToHit;
    10.     public GameObject playerObject;
    11.     private bool m_FacingRight = true;
    12.     public Transform BulletTrailPrefab;
    13.     public Transform MuzzleFlashprefab;
    14.     float timetoSpawnEffect = 0;
    15.     public float effectSpawnRate = 10;
    16.     private float move;
    17.  
    18.     float timeToFire = 0;
    19.     public Transform firePoint;
    20.  
    21.     // Use this for initialization
    22.     void Awake() {
    23.         Transform firePoint = transform.FindChild("firePoint");
    24.         if (firePoint == null) {
    25.             Debug.LogError("No FirePoint? what?!?");
    26.  
    27.             {
    28.             }
    29.  
    30.         }
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update() {
    35.         if (fireRate == 0) {
    36.             if (Input.GetButtonDown("Fire1")) {
    37.                 Shoot();
    38.             }
    39.         }
    40.         else {
    41.             if (Input.GetButton("Fire1") && Time.time > timeToFire) {
    42.                 timeToFire = Time.time + 1 / fireRate;
    43.                 Shoot();
    44.                 if (move < 0)
    45.                 {
    46.                     m_FacingRight = false;
    47.                     transform.localRotation = Quaternion.Euler(0, 180, 0);
    48.                 }
    49.                 else
    50.                 {
    51.                     m_FacingRight = true;
    52.                     transform.localRotation = Quaternion.Euler(0, 0, 0);
    53. }
    54.             }
    55.         }
    56.     }
    57.     private void Shoot()
    58.     {
    59.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    60.         Vector2 shootDirection = (mousePosition - (Vector2)firePoint.position).normalized;
    61.         Ray2D ray = new Ray2D(mousePosition, shootDirection);
    62.  
    63.         RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 100, whatToHit);
    64.  
    65.         if (hit.collider != null)
    66.         {
    67.             Debug.DrawLine(ray.origin, hit.point, Color.green);
    68.             Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
    69.         }
    70.         else
    71.         {
    72.             Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
    73.         }
    74.  
    75.         if (Time.time >= timetoSpawnEffect)
    76.         {
    77.             Effect();
    78.             timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
    79.         }
    80.     }
    81.     private void Effect() {
    82.         Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
    83.         Transform Clone = Instantiate (MuzzleFlashprefab, firePoint.position, firePoint.rotation) as Transform;
    84.         Clone.parent = firePoint;
    85.         float size = UnityEngine.Random.Range(0.7f, 0.9f);
    86.         Clone.localScale = new Vector3 (size, size, size);
    87.         Destroy (Clone.gameObject, 0.2f);
    88.     }
    89. }
    90.  
     

    Attached Files:

    • Bug.png
      Bug.png
      File size:
      9.8 KB
      Views:
      699
  2. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    Have you tried inverting the direction?

    Code (csharp):
    1. Vector2 shootDirection = (mousePosition + (Vector2)firePoint.position).normalized;
    Change the '-' to a '+'. What is the result?
     
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I have a little class that will help you with selected objects

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. public class Utilities2D
    6. {
    7. /// <summary>
    8. ///  This will return a 2D gameobject ONLY using orthographic projection
    9. ///  use the 3D version if your object is a 3D object but is in a 2D world.
    10. /// </summary>
    11. /// <returns>selected gameobject otherwise null</returns>
    12. public static GameObject SelectObject()
    13. {
    14.   GameObject mObject = null;
    15.   Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.   RaycastHit2D mHit = Physics2D.Raycast(mRay.origin, Vector2.zero, Mathf.Infinity);
    17.   if (mHit != null && mHit.collider != null)
    18.    mObject = mHit.collider.gameObject;
    19.  
    20.   return mObject;
    21. }
    22. }
    23.  
     
    Croomraider likes this.
  4. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36

    Attached Files:

    • Bug.png
      Bug.png
      File size:
      26.5 KB
      Views:
      714