Heyyo guys !
So i've created this code right here, and i wanted to do it so my enemies can inherit the movement and rotation code i've created. But, since im not good with inheritance and so forth it gives me all sorts of errors.
using UnityEngine;
using System.Collections;
public class Enemies : MonoBehaviour {
Transform playerTransform;
Transform myTransform;
protected int Health;
protected int MoveSpeed;
protected int Attack;
protected int MovementSpeed;
protected int RotationSpeed;
protected float DetectionRange;
void Awake() {
myTransform = transform;
}
void Start () {
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update () {
if (Vector3.Distance(playerTransform.position, myTransform.position) < DetectionRange)
{
//Rotate to player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), RotationSpeed * Time.deltaTime);
//Move to player
myTransform.position += myTransform.forward * MovementSpeed * Time.deltaTime;
}
}
}
Now these all works well if a put this script onto a gameobject, and give values to movementspeed, detection and rotation. But the moment i do this:
void Update () {
movement();
}
void movement () {
if (Vector3.Distance(playerTransform.position, myTransform.position) < DetectionRange)
{
//Rotate to player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), RotationSpeed * Time.deltaTime);
//Move to player
myTransform.position += myTransform.forward * MovementSpeed * Time.deltaTime;
}
}
(I moved my movement and rotation code in a separate function). The reason for why im doing this is, that enemies should inherit these traits, and therefore making it easier for me to create enemies my adjusting the variables RotationSpeed, MovementSpeed and DetectionRange for each enemy that inherits this.
In general i want this function problem solved, so i can begin to create script that inherit these traits properly, but i'm having a hard time figuring it out. Could you help me ?
Kind Regards,
Lars.
↧