Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How do i make a projectile follow a target?

Discussion in 'Scripting' started by BL4CK0UT19, Feb 20, 2020.

  1. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    75
    So,im making a shooting game.

    My projectile/bullet code works fine. I can easily instantiate it and make it move along the direction the gun was facing.
    But i would like to create a bullet that follow a certain enemy,for example:

    After it is instantiated,it will move along the direction the gun was facing,and then,if a enemy come close to it (maybe using a big circle collider),it will change it's path to follow the enemy (in real time,if the enemy tries to run,it needs to follow it).

    How can i do that?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    The first thing you need to know is, what target? Is it going to automatically find a target that's close to its path? Will it be chosen when the player fires? However you find your target, store that Transform in a variable.

    Once you have that, you now need to decide how you will turn to follow the enemy. Is it going to always point directly towards the target no matter how fast the target moves? If so, you can just use transform.LookAt(yourTarget.position).
    If you'd prefer it has a limit to how fast it can turn, then you'll need to do a little bit more math, something like:
    Code (csharp):
    1. Vector3 directionToTarget = yourTarget.position - transform.position;
    2. Vector3 currentDirection = transform.forward;
    3. float maxTurnSpeed = 60f; // degrees per second
    4. Vector3 resultingDirection = Vector3.RotateTowards(currentDirection, directionToTarget, maxTurnSpeed * Mathf.Deg2Rad * Time.deltaTime, 1f);
    5. transform.rotation = Quaternion.LookRotation(resultingDirection);
     
    HeavyViolence and BL4CK0UT19 like this.
  3. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    i would use physics overlap sphere to detect enemy proximity, then change projectile path to follow enemy at set distance, but overtime moving closer to enemy maybe
     
    BL4CK0UT19 likes this.
  4. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    75
    Hey friend,thanks for the answer.
    It does not need to have a limit to its speed,because it's actually a pretty simple game.
    How can i implement "transform.LookAt(yourTarget.position)"?

    ''yourTarget'' needs to be a gameObject,and i need to assign it in the inspector?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    A Transform rather than a GameObject, but that's a small detail. In order to answer this I need the answers to the questions from my first comment:
    I can help you figure out how to turn your intentions into code but I have no idea what your intentions are.
     
  6. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    75
    Hey friend
    Sorry for taking so long to answer.
    Had some problems here,but im still at the same place.

    So,my intentions:
    There will be some target enemies that that walk and move. Then,there is a projectile that needs to follow these targets.
    These projectiles are bullets from an weapon. When fired,the projectile travels in a straight line (like any other projectile),but when it gets too close to a target,it will change it's path to hit it.

    I was able to progress a little bit:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletFollowEnemy : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.  
    9.     public float speed = 0.02f;
    10.  
    11.     private Transform target;
    12.  
    13.     private float moveSpeedFollow = 1f;
    14.     public float rotateSpeed = 200f;
    15.  
    16.  
    17.  
    18.     public float MoveSpeed5 = 1f;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.  
    24.         target = GameObject.FindGameObjectWithTag("EnemyToFollow").transform;
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.  
    32.      
    33.  
    34.      
    35.        
    36.          
    37.  
    38.             Vector3 direction = (Vector3)target.position - rb.position;
    39.  
    40.             direction.Normalize();
    41.  
    42.             Vector3 rotateAmount = Vector3.Cross(direction, transform.forward);
    43.  
    44.             rb.angularVelocity = -rotateAmount * rotateSpeed;
    45.  
    46.             rb.velocity = transform.forward * speed;
    47.  
    48.         }
    49.      
    50.  
    51.     }
    52.  
    By following this tutorial i found on youtube,i was able to make the projectile follow a object by it's tag. But i have little to no control over it,for example: if i have two objects with the same tag,the projectile will ALWAYS go to the last one created. Also,the projectile goes out of the gun already towards the target,and i really need to snap only after it gets too close.

    But this code i made is like 80% copied from the video,so,if you have anything in your mind,i can definitely start it over .
     
  7. pein1905

    pein1905

    Joined:
    Apr 7, 2024
    Posts:
    1
    Thanks for your answer, it solve my problem. Also, simply change FindGameObjectWithTag to FindGameObjectsWithTag will return an array of matched tag GO
     
    Last edited: May 24, 2024