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

Setting Target For Enemy Inside AI?

Discussion in 'Scripting' started by ecloev, Oct 13, 2015.

  1. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Hello,
    So I have a transform target and I need to set the target to ym main camera?
    How can I do this?
    This code gives me the cannot implicitly convert transform into GameObject.
    Thanks

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AdvancedAI : MonoBehaviour
    6. {
    7.     float distance;
    8.    
    9.     public float lookAtDistance = 25.0F;
    10.     public float chaseRange = 2050.0F;
    11.     public float attackRange =4.5F ;
    12.     public float moveSpeed = 5.0F;
    13.     public float damping = 6.0F;
    14.     public float damage = 30.0F;
    15.     public float attackRepeatTime = .8F;
    16.     public Transform target;
    17.     public CharacterController controller;
    18.     private float verticalMomentum = 0f;
    19.     private float gravity = -9.8f;
    20.    
    21.     private Vector3 moveDirection = Vector3.zero;
    22.     private float attackTime;
    23.  
    24.  
    25.  
    26.     //TODO: add in a function to find controller and to locate and assign the player as the target
    27.    
    28.     void Start()
    29.     {
    30.         attackTime = Time.time;
    31.         target = GameObject.FindWithTag("MainCamera");
    32.     }
    33.    
    34.     void Update()
    35.     {
    36.         distance = Vector3.Distance(target.position, transform.position);
    37.         verticalMomentum += gravity * Time.deltaTime;
    38.        
    39.         if(distance < lookAtDistance)
    40.         {
    41.             LookAt();
    42.         }
    43.        
    44.         if (distance < attackRange)
    45.         {
    46.             AttackPlayer();
    47.         }
    48.        
    49.         else if (distance < chaseRange)
    50.         {
    51.             ChasePlayer();
    52.  
    53.         }
    54.     }
    55.    
    56.     void LookAt()
    57.     {
    58.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    59.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    60.     }
    61.    
    62.     void ChasePlayer()
    63.     {
    64.         moveDirection = transform.forward;
    65.         moveDirection *= moveSpeed;
    66.         moveDirection.y += verticalMomentum * Time.deltaTime;
    67.         controller.Move(moveDirection * Time.deltaTime);
    68.     }
    69.    
    70.     void AttackPlayer()
    71.     {
    72.         //TODO: Need Attack Animations
    73.         if (Time.time > attackTime)
    74.         {
    75.             target.SendMessage("damagePlayer", damage, SendMessageOptions.DontRequireReceiver);
    76.             attackTime = Time.time + attackRepeatTime;
    77.         }
    78.     }
    79.    
    80.     void ApplyDamage()
    81.     {
    82.         chaseRange += 15;
    83.         moveSpeed += 1;
    84.         lookAtDistance += 20;
    85.     }
    86. }
    87.  
     
  2. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Any Ideas guys?
    If I get this, i'm done my game :)
     
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Code (CSharp):
    1. target = GameObject.FindWithTag("MainCamera").transform;
     
  4. gutsav

    gutsav

    Joined:
    Oct 13, 2015
    Posts:
    1
    your problem is that the target is a Tranform and it needs to be a game object
     
  5. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    If you shooting at the player to make him get hit when moving just saying. I would just make a 2d/3d cube and make it
    transparrent. Then attach it to the target

    public game object Target;
    drag the cube to the target attachment
    place the cube anywhere you want. and attach it
    target = Gameobject.Target;
    Ta-Da!