Hey all, so I am programming a game where there are enemies that follow the player. How to I program it so that when the enemies make contact, the player dies?
Current script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AIController : MonoBehaviour
{
public Transform Player;
int MoveSpeed = 4;
int MaxDist = 10;
int MinDist = 5;
void Start()
{
}
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
}
What does the player dying mean in your game? There is no built on concept of death in Unity or C#. You have to figure out what that means in your game.
After you figure that out, you write the code you want to call when the player dies. After you’ve written that code, you then call that code in an OnCollisionEnter method (or otherwise detect contact between the player and the enemies).
This will literally end the game, but I assume you’re looking for something different:
public void GameToEnd()
{
Application.Quit();
}
Does ending the game mean some cut scene? Popping up a window with some gameplay stats? Restarting the game from a save point? Going back to the main menu? Once you figure out what it actually means for your game you write the code to actually do it, then call it on collision with these enemies.