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

Raycast problem

Discussion in 'Editor & General Support' started by Drak50, Aug 22, 2014.

  1. Drak50

    Drak50

    Joined:
    Jul 14, 2014
    Posts:
    4
    Hi all,

    I am in a gaming class that we are making a very basic FPS. In the game the enemies just move forward and will change directions when it hits an object or another enemy. My problem though is that the enemies are just going right through the objects and wont change directions. I have the collider boxes selected on each of the enemies, buildings and walls. The enemies has a tag of ENEMY and the buildings and walls has a tag of BUILDING. If anyone can let me know if I am doing something wrong it would be appreciated. Here is my code for the enemies.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Enemy : MonoBehaviour {
    6.  
    7.     //variables
    8.     public int EnemySpeed = 30;
    9.     public bool IsAlive = true;
    10.     private RaycastHit HitThing;
    11.     public float TurnDistance = .05f;
    12.  
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.    
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.    
    23.         if (IsAlive == true)
    24.         {
    25.             transform.Translate (Vector3.forward * EnemySpeed * Time.deltaTime);
    26.         }
    27.         else
    28.         {
    29.             Destroy (gameObject);
    30.         }
    31.         if (Physics.Raycast(transform.position, Vector3.forward, out HitThing, TurnDistance))
    32.         {
    33.             if(HitThing.collider.tag == "BUILDING" || HitThing.collider.tag == "ENEMY")
    34.             {
    35.                 transform.Translate (0,0,-10 * EnemySpeed * Time.deltaTime);
    36.                 transform.Rotate (0,90,0 * EnemySpeed * 5 * Time.deltaTime);
    37.             }
    38.         }
    39.  
    40.     }
    41. }
    42.