Make enemy's weapon chase a game object
Would it be possible if anyone could help me with a code that makes it where the enemy can target a game object, even if the game object is moving? I am trying to make a game object that targets a player.
Or in simpler words, make a homing bullet
Answer by prefix · Jan 23, 2017 at 01:48 PM
Check out this link.
Or review this script, courtesy of Unity!
*// This complete script can be attached to a camera to make it
// continuously point at another object.
// The target variable shows up as a property in the inspector.
// Drag another object onto it to make the camera look at it.*
using UnityEngine;
using System.Collections;
*// Create C# script and name is whatever you want. Make sure to change the name "ExampleClass"
// to whatever the name of your script is if you plan to copy and paste.*
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}
Put this script on the enemy gun or enemy parent, whichever works best in your game. Then drag the game object (player) into the empty transform variable (called target) in the inspector.
Your answer
Follow this Question
Related Questions
Shoot towards target 0 Answers
Why doesnt Gameobject.find not work? 3 Answers
GameObject.Find(CaseInsensetive) ? 1 Answer
Have enemy constantly try to get in front of player and push them back 0 Answers
Getting a pivoted look at in 2D 2 Answers