Tageos
1
Hello!
I am trying to give the enemy movement in the direction of the player using this:
transform.position += playerPos.normalized * 10 * Time.deltaTime;
Its not working quite well though, the enemy moves in reverse y-direction or something.
This is the rest of the code:
using UnityEngine;
using System.Collections;
public class ninjaScript : MonoBehaviour {
//Ninja spawn phase
private Vector3 ninjaSpawn;
private bool hasSpawned = true;
public bool slidDone;
public bool hasSlid;
public int dice;
public float slideSpeed;
//Ninja attack phase
public bool attackDone;
public bool hasAttacked;
public Vector3 playerPos;
private Vector3 targetPos;
void Start () {
dice = Random.Range (-3, 3);
hasSpawned = true;
}
void Update () {
if (!hasSlid){
slide ();
}
if (hasSlid){
attackCycle ();
}
}
void slide(){
transform.Translate(0,-slideSpeed*Time.deltaTime,0);
if(this.gameObject.transform.position.y < dice){
slidDone = true;
}
if (slidDone){
hasSlid = true;
}
}
void attackCycle (){
if (!hasAttacked){
attack ();
}
if (attackDone){
hasAttacked = false;
}
}
void attack () {
playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
transform.position += playerPos.normalized * 10 * Time.deltaTime;
}
}
If someone has any tips id really appretiate it!
Your means of moving towards the player’s position is flawed.
What you’re adding is the player’s absolute position.
What you want is to add the player’s relative position. The direction from vector A to vector B is calculated as B - A. Therefore, it’s a fairly simple change you’re needing to make:
transform.position += (playerPos - transform.position).normalized * 10 * Time.deltaTime;