Move a object along a vector
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!
Answer by Eno-Khaon · Feb 13, 2016 at 11:26 PM
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;
I forgot to mention that i want to make the enemy keep moving if he misses the player. This solution makes him stop once reaching the player. Do you think i have to rethink the solution or can i just modify transform.position += (playerPos - transform.position).normalized * 10 * Time.deltaTime;
?
"misses the player" is unlikely, as you're calling this every frame. Any overshooting will be accounted for on the very next frame with a new direction, sending your object back towards its target.
If you want your object capable of "missing" its target, a few options are:
1- Poll the target's location at some time and move towards that fixed location. 2- Have your object move in a fixed direction and adjust its direction by a certain amount each update in order to eventually face your target.
Thank you for the tips, i have been investigating option #1 but i cant really find a good method without using rigidbody. Because the enemy eventually stops (when hitting a wall) i thought of using the vector3.movetoward with a target point on the wall. It got real messy when i tried to pinpoint the point on the wall (using the equation for a line y = ax+ b and the player position). there has to be a way to call a function once that tells the enemy to move along a vector..
You can create a variable for the direction, set it to (playerPos - transform.position).normalized
, then just use that variable to move in a fixed direction at a time. As an example of this:
public float resetTime = 3.0f;
float currentTime = 0.0f;
Vector3 moveDirection;
void Update()
{
currentTime += Time.deltaTime;
if(currentTime >= resetTime)
{
currentTime = 0.0f;
moveDirection = (playerPos - transform.position).normalized;
}
}
Your answer
Follow this Question
Related Questions
Movement Sticking 0 Answers
How to make more realistic bot AI? 2 Answers
C# AI move on Sight 1 Answer
Reversing movement directions 1 Answer
transform.position is not updating the object's position 1 Answer