Character leans back or forward based on distance
Question: … ?
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Enemy : MonoBehaviour
{
public float movementSpeed = 5f;
public GameObject respawnPoint;
private Transform player;
public float respawnDelay = 3f;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
Vector3 direction = player.position - transform.position;
direction.Normalize();
transform.rotation = Quaternion.LookRotation(direction);
transform.position += direction * movementSpeed * Time.deltaTime;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
// Reload the current scene
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
if (collision.gameObject.CompareTag("Bullet"))
{
Respawn();
}
}
private void Respawn()
{
// Move the enemy to the respawn point
transform.position = respawnPoint.transform.position;
// Reset any other enemy state or variables as needed
// You can also reload the scene instead of just respawning the enemy
// SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}