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

shooting ray at right direction

Discussion in 'Scripting' started by eleon-games, Apr 7, 2017.

  1. eleon-games

    eleon-games

    Joined:
    Feb 19, 2015
    Posts:
    56
    Hi,

    i want to shoot a ray in the direction my 2d player (with 3d collider) is facing
    So the facing of the sprite changes x=1 / x=-1 if i press left/right

    i want to say if Scale X = 1 then shoot ray left
    and if Scale X = -1 then shoot the ray right

    i can shoot the ray but can't handle the facing.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.  
    4.         if (transform.localScale.x= 1)  //<here i don't know how to type it correct. The rest seems to work.
    5.             {
    6.         Ray ray = new Ray (transform.position, -transform.right);
    7.         Debug.DrawRay (ray.origin, ray.direction, Color.red);
    8.             }
    9.         else
    10.  
    11.         {
    12.             Ray ray = new Ray (transform.position, transform.right);
    13.             Debug.DrawRay (ray.origin, ray.direction, Color.blue);
    14.         }
    15.  
    16.     }
    17. }
     
    Last edited: Apr 7, 2017
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    Code (CSharp):
    1. if (transform.localScale.x == 1)// == sign checks if one value is equal to the other. = sign assigns value on the right of it to variable on its left.
    2.         {
    3.             Ray ray = new Ray(transform.position, -transform.right);
    4.             Debug.DrawRay(ray.origin, ray.direction, Color.red);
    5.         }
    6.         else
    7.  
    8.         {
    9.             Ray ray = new Ray(transform.position, transform.right);
    10.             Debug.DrawRay(ray.origin, ray.direction, Color.blue);
    11.         }