2D movement along vector
Basically I'm making turn-based topdown game, and I need to get Boss (enemy) move in player direction. I used
transform.position = Vector3.MoveTowards(transform.position, destinationPosition, moveSpeed * Time.deltaTime);
so far, but it has a flaw - it stops at player location. Instead I want it to keep moving in the same direction regardless of reaching player (basically to overshoot player location). Movement is done with moveset functions, each of them has it's own moves and conditions to stop/keep moving. One I'm working now is supposed to move boss X amount of units alongside vector pointing from original boss position and towards player.
Answer by Jessespike · Jun 09, 2016 at 08:27 PM
Get a normalized vector of the transform to the target. Update position based on that.
public Transform target;
public float moveSpeed = 1f;
Vector3 moveVector;
// Use this for initialization
void Start () {
CalculateMoveVector(target);
}
public void CalculateMoveVector(Transform target) {
moveVector = (target.position - transform.position).normalized;
}
// Update is called once per frame
void Update () {
transform.position += moveVector * moveSpeed * Time.deltaTime;
}
Nice, it somewhat works, but I have problems now adapting it for my code - I need it to pick player position every turn, not just once on startup. And so far my attempts only lead to breaking it.
Your answer
Follow this Question
Related Questions
I can't make my character move 0 Answers
How to move a game object to a position after selecting it 0 Answers
(Help) I am having trouble with creating a simple 2D game in Unity. Please help. :) 0 Answers
Why won't my 2D Sprite Move? 1 Answer
2D Object following the cursor without glitchyness at the center 0 Answers