Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

make the enemy shoot the player ?

Discussion in 'Getting Started' started by Quast, Mar 4, 2016.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    458
    Again i need help from you guys. Its really good to find like this commUNITY.
    OK. i have a code that enemy follow the target and i add ability for the enemy to shoot the target.

    the problem is the how to make the enemy faccing the target when he shot so the bullet hit him.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class efollow : MonoBehaviour {
    6.     public Transform target;
    7.     public Transform mytransform;
    8.     public GameObject enemy;
    9.     public float elife = 100f;
    10.     GameObject ebullet;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         ebullet = Resources.Load("bullets") as GameObject;
    15.  
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         transform.LookAt(target);
    21.         transform.Translate(Vector3.forward * 5 * Time.deltaTime);
    22.  
    23.  
    24.             GameObject bullets = Instantiate(ebullet) as GameObject;
    25.             bullets.transform.position = transform.position + target.transform * 1;
    26.             Rigidbody rb = bullets.GetComponent<Rigidbody>();
    27.             rb.velocity = target.transform * 10;
    28.             Destroy(bullets, 9f);
    29.  
    30.        
    31.  
    32.         if(elife <=0 )
    33.         {
    34.             enemy.SetActive(false);
    35.         }
    36.  
    37.  
    38.  
    39.     }
    40.  
    41.  
    42.     void OnTriggerEnter(Collider other)
    43.     {
    44.         if (other.gameObject.CompareTag("bulleter"))
    45.         {
    46.             elife -= 50;
    47.         }
    48.     }
    49.  
    50.  
    51.  
    52. }
    53.  
    54.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,188
    Looks to me like that transform.LookAt(target) line should already make it face the target. Is that not happening? What happens instead?
     
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    458
    The enemy follow me.OK no problem with this.but he shoot to the front direction of the player not to the player.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,188
    I'm not sure I understand you. But I notice your code is not setting the rotation of the bullet. I would expect to see something like:

    Code (CSharp):
    1. bullets.transform.rotation = transform.rotation;
    ...assuming your bullets are scripted to move forward.

    Also, I see you're using Rigidbody and physics on your projectile. There's a lot of that going around lately, but I don't recommend it — it makes everything harder than it needs to be. Consider removing the Rigidbody, and just moving the transform with scripts.
     
  5. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    458
    OK.
    Can you fix it for me. Please ?
    If i want my enemy to shoot a certain point what do i code ?
     
  6. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    363
    Forgive me for asking a silly question, but is this line right?

    Code (CSharp):
    1.      rb.velocity = target.transform * 10;
    For example, if the enemy is at -1, -1, but the player is at 1, 5 surely the bullet will be going in the wrong direction because you're setting the Velocity to -1, -1, right? Or am I wrong, lol?

    Perhaps you'd want something like this:

    Code (CSharp):
    1.  rb.AddForce(bullets.transform.forward * bulletforce); // set bulletforce to whatever you need/want
    This will send the bullet along it's FORWARD trajectory with a set force.

    You'd also want the bullet to be looking at the player as well (I don't know what orientation it's gonna spawn in at) so you might want to use this line just above the rb.addforce one.

    Code (CSharp):
    1. bullets.transform.LookAt(target);
     
  7. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    458
    I don't know why but what you said did not give me the result.
    I do solve the problem by do this
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class efollow : MonoBehaviour {
    5.     public Transform target;
    6.     public Transform mytransform;
    7.     public GameObject enemy;
    8.     public float elife = 100f;
    9.      GameObject ebullet;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         ebullet = Resources.Load("bullets") as GameObject;
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         transform.LookAt(target);
    20.         transform.Translate(Vector3.forward * 5 * Time.deltaTime);
    21.  
    22.         GameObject bullets = Instantiate(ebullet) as GameObject;
    23.         bullets.transform.position = transform.position * 1;
    24.      
    25.         Rigidbody rb = bullets.GetComponent<Rigidbody>();
    26.         rb.velocity = transform.forward * 20;
    27.         Destroy(bullets, 9f);
    28.  
    29.         if (elife <=0 )
    30.         {
    31.             enemy.SetActive(false);
    32.         }
    33.  
    34.  
    35.  
    36.     }
    37.  
    38.     void OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.gameObject.CompareTag("bulleter"))
    41.         {
    42.           //  other.gameObject.SetActive(false);
    43.             elife -= 50;
    44.         }
    45.     }
    46.  
    47. }
    48.  
    Now my enemy shooting and follow me :)
    His keep shooting me so i need to add timer or something that let him stop for a second than shoot me again.
    how to do this ? any advice will be really helpful.

    you right. there is things need to recheck in my codes. i add rigidbody so that i can play with the bullet. ((add things later))
     
    Tset_Tsyung likes this.
  8. Asherhero

    Asherhero

    Joined:
    Aug 8, 2019
    Posts:
    5
    how to make an enemy shoot 100 bullets and then wait to reload
     
  9. A_Person_Named_Dart

    A_Person_Named_Dart

    Joined:
    Apr 14, 2018
    Posts:
    330
    might want to ask ur own question for that