Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Casting a 2D Ray downwords

Discussion in 'Scripting' started by realvasion, Apr 27, 2015.

  1. realvasion

    realvasion

    Joined:
    Oct 25, 2014
    Posts:
    21
    It’s a 2d platform game where the camera shoots the lasers downwards and if it hits the player the player will lose health otherwise if it hit enemy than it should no deduct the enemies health here is a visual image of what I want In order to cast the ray from camera to (downwards) I have to put a negative value to the y axis of variable theYAxies and if I do that it simply wont work it only work if I put a positive value to theYAxies which in this case (from camera to upward)

    Here is what I have coded
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Grids : MonoBehaviour {
    5.     public int shootLayer ;
    6.     public Transform rayTouchPoint;
    7.     public float rayLenght;
    8.     public Vector2 theYAxies;
    9.     public GameObject hitObject;
    10.     public Board board;
    11.     // Use this for initialization
    12.     void Awake ()
    13.     {
    14.         shootLayer = LayerMask.GetMask("Laser");
    15.         theYAxies = new Vector2(0f,-10f)
    16.     }
    17.  
    18.     // Update is called once per frame
    19.  
    20.  
    21.     void CastRay()
    22.     {
    23.         RaycastHit2D hit = Physics2D.Raycast(rayTouchPoint.position,theYAxies,rayLenght,shootLayer);
    24.         if ( hit.collider != null )
    25.         {
    26.             hitObject = hit.collider.gameObject;
    27.             Debug.Log( "You hit on " + hit.collider.name +  " at layer is " + hit.transform.gameObject.layer);
    28.         }
    29.         Debug.DrawRay(rayTouchPoint.position,theYAxies * rayLenght, Color.red);
    30.     }
    31.  
    32. }
    33.  
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    It works, if you put a positive value in it? So, I think your object is upside down? Did you try to draw a debug ray to see where it is going when it is shooting with a negative value.
     
  3. realvasion

    realvasion

    Joined:
    Oct 25, 2014
    Posts:
    21
    thanks for reply,you are right it seems like its upside down but how do I fix it, because if I put a positive value than it goes from bottom to up (I don’t want to put the laser camera on the floor to shoot upward), but if I put a negative value it didn’t even detect the hit object, so any help plz?
     
  4. realvasion

    realvasion

    Joined:
    Oct 25, 2014
    Posts:
    21
    doesn't matter I solve it by my self:cool: I just set the Y value of theYAxies to 1, which it just draw the laser upward and in order to draw laser downword I just put a nagative value to the length of ray. that's it it work now:D