Search Unity

raycasthit doesnt work

Discussion in 'Scripting' started by ghost123_1234, May 25, 2015.

  1. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    hello

    I have this script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.     public Transform sightStart, sightEnd;
    7.  
    8.     public Animation idle;
    9.  
    10.     RaycastHit2D interacted;
    11.  
    12.     public RaycastHit hit;
    13.  
    14.     public float speed = 1.0f;
    15.  
    16.     float jumpTime, jumpDelay = .3f;
    17.     bool jumped;
    18.  
    19.     Animator anim;
    20.  
    21.     public bool animation = true;
    22.  
    23.     public bool spottedenemy = false;
    24.  
    25.     void Start()
    26.     {
    27.         anim = GetComponent<Animator>();
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         transform.Translate(Vector3.right * speed * Time.deltaTime);
    33.         Debug.DrawLine(Vector3.zero, Vector3.right, Color.red);
    34.         Movement(); //call the function every frame
    35.         RaycastStuff(); //call the function every frame
    36.        
    37.         if(Physics.Raycast (transform.position, transform.right, out hit, 100))
    38.         {
    39.             Debug.DrawLine(transform.position, transform.forward);
    40.             if (hit.collider.gameObject.tag == "Enemy")
    41.             {
    42.                 speed = 0;
    43.                 Debug.Log("geraakt");
    44.                 hit.transform.SendMessageUpwards("ApplyDamageToEnemy", 5);
    45.             }
    46.         }
    47.     }
    48.  
    49.     void RaycastStuff()
    50.     {
    51.         //Debug.DrawLine(sightStart.position, sightEnd.position, Color.green);
    52.         //spottedenemy = Physics2D.Linecast(sightStart.position, sightEnd.position,1<< LayerMask.NameToLayer("Enemy"));
    53.        
    54.     }
    55.  
    56.  
    57.     void Movement() //function that stores all the movement
    58.     {
    59.        
    60.  
    61.         if(animation == true && spottedenemy == false)
    62.         {
    63.             speed = 2;
    64.            // anim.SetFloat("speed", Mathf.Abs (-10F));
    65.            
    66.             //transform.eulerAngles = new Vector2(0, 0); //this sets the rotation of the gameobject
    67.            
    68.         }
    69.         else if(spottedenemy == true)
    70.         {
    71.  
    72.             //animation = false;
    73.             speed = 0;
    74.            
    75.            
    76.            
    77.             //anim.SetFloat("speed", 0);
    78.  
    79.            
    80.         }
    81.         else if(spottedenemy == false)
    82.         {
    83.             animation = true;
    84.         }
    85.  
    86.      
    87.    
    88.  
    89.  
    90.     }
    91. }
    92.  
    but for some reason
    it doesnt work i am sure i have the right tag but also the debug.log when it sees Enemy doesnt work
    when i debug the raycast i have a weird problem
    maybe that is the cause but i dont know how to fix it

    the red line stays there it doesnt go with the grey cube that is the player
    this script above is on the player
     
  2. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    I'm not entirely sure wheat you are trying to achieve here, sorry :)

    But this line confuses me:

    Code (csharp):
    1. Debug.DrawLine(transform.position, transform.forward);
    thansform.forward is not a position, but a direction (local z axis direction in worldspace).
    So if you want to draw a line from the transform position, and then in the direction of the forward vector, you would write something like:

    Code (csharp):
    1. Debug.DrawLine(transform.position, transform.position+transform.forward*lengthOfLine);
    Now, as said I don't know your end goal here, but your debug line doesn't really make any sense to me, as it will always just draw a line from the position of the object to a position one unit from the origin of your worldspace. Which just has to be unintended ;)
     
  3. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    i want that my player so the grey cube is looking forward with a raycast
    so my player moves and a raycast needs to move out to that direction to see if there is any object in front of it
    and when it sees a object with the tag "Enemy" it shoot set the speed to 0

    i know that forward it the right direction but for some reason it doesnt see the Enemy
    (red cube)
     
  4. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Ok, now I get your intentions;) That still doesn't really make your debug lines make any sense.

    1) Your first debug line will always draw the same place, and will not move with your grey block. Is that your intention? If not, you have to make it relate to the objects position.

    2) Are your SURE your object isn't rotated, and the axis you are looking at is the world axis? Check if your pivot is set to Global or Local. You are using the objects local x-axis (in world space), and so you have to make sure that is the right direction.

    3) Your second debug line will always draw a line from the object's position, and to a position near the origin of world space. NOT forward. If you want it to draw a line forward, you have to add the objects position to the forward vector.