Reputation: 39
BasicEnemy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicEnemy : MonoBehaviour
{
public Transform target;
public float speed = 3f;
public float attack1Range = 1f;
public int attack1Damage = 1;
public float timeBetweenAttacks;
// Use this for initialization
void Start()
{
Rest();
}
// Update is called once per frame
void Update()
{
}
public void MoveToPlayer()
{
//rotate to look at player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0, -90, 0), Space.Self);
//move towards player
if (Vector3.Distance(transform.position, target.position) > attack1Range)
{
transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
}
}
public void Rest()
{
}
}
EnemyTerritory
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTerritory : MonoBehaviour
{
public BoxCollider territory;
GameObject player;
bool playerInTerritory;
public GameObject enemy;
BasicEnemy basicenemy;
// Use this for initialization
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
basicenemy = enemy.GetComponent<BasicEnemy>();
playerInTerritory = false;
}
// Update is called once per frame
void Update()
{
if (playerInTerritory)
{
basicenemy.MoveToPlayer();
}
if (playerInTerritory)
{
basicenemy.Rest();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerInTerritory = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
playerInTerritory = false;
}
}
}
Any feedback is welcome I trying to get enemies I added to the game to move toward the playership as if to attach and follow once I can get this done with the movement I will get them to shoot at the playership. right now all the new enemies I added are spinning really fast in circles. I get no syntax errors.
Upvotes: 0
Views: 68
Reputation: 26
There's definitely something fishy in the Update function because you're assigning (=
) instead of checking for equality (==
) in the condition.
Didn't you mean playerInTerritory == true
?
void Update()
{
if (playerInTerritory)
{
basicenemy.MoveToPlayer();
}
else
{
basicenemy.Rest();
}
}
If that doesn't help, add some Debug.Log
statements in the trigger enter/exit functions to see when they are being called.
Alright, I looked into the spinning. If you want the enemy to follow the player until it's within the attack range, you have both the wrong rotation and the wrong translation.
// Rotate to look at player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0, -90, 0), Space.Self);
What this does, it makes the enemy turn towards the player (which you want) and the second line makes the enemy spin (90° anticlockwise each frame). If you don't want the enemy to spin, you don't need the line. Just keep the 'Look At' function.
The translation problem is that you move in the direction of the X axis – which is not the direction of the player. You should calculate the direction vector first and translate along it.
The gist of it is always:
// normalized, so the vector affects only direction, not speed
Vector3 direction = (FROM.position - TO.position).normalized;
So, there:
Vector3 enemyToTargetDirection = (target.position - transform.position).normalized;
enemyToTargetDirection.y = 0; // stay in the same height
transform.Translate(enemyToTargetDirection * speed * Time.deltaTime);
You could do it like this, but I think it looks better when the enemy rotates towards you gradually, instead of just 'snapping' in your direction. In that case, the resulting function would look like this:
public void MoveToPlayer()
{
Vector3 upwardsAxis = Vector3.up;
float distanceToPlayer = Vector3.Distance(transform.position, target.position);
if (distanceToPlayer > attack1Range)
{
float step = Time.deltaTime * speed;
Vector3 lookDirection = target.position - transform.position;
if (lookDirection != Vector3.zero) // Prevents rotation errors
{
// Rotate over time
Quaternion lookRotation = Quaternion.LookRotation(lookDirection, upwardsAxis);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, step * 2);
}
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
Oh, and by the way, for movement/rotation/physics in general, you should be using FixedUpdate()
instead of the regular Update()
(it makes the movement more fluent) and don't keep 'Awake', 'Start', 'Update' methods around if they are blank, since it can affect performance (slightly).
Upvotes: 1