Hi, I’m using Unity 4 and spawning an object in the same position every second. Each time an object is spawned, a vector is drawn from the object to moveTowardsPos (the position of my player-character). I use the code in the Start() method to mark where I want my vector to be drawn to and the code in the Update() method to draw two vectors.
void Start () {
player = GameObject.FindObjectOfType<Player>();
moveTowardsPos = player.transform.position;
Vector2 direction = player.transform.position - transform.position;
float angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg + 270;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 100.00f * Time.deltaTime);
}
void Update () {
Debug.Log (moveTowardsPos);
Debug.DrawLine(this.gameObject.transform.position, moveTowardsPos, Color.green);
Debug.DrawLine(this.gameObject.transform.position, moveTowardsPos * 2.0f, Color.red);
}
I’m drawing a green vector that starts at my object and ends at moveTowardsPos (the position of my player-character). I want the red vector to be in the same direction as the green vector, but just twice as long.
However, this is what happens when I play the game: https://i.imgur.com/z8S7IgN.png
The red vector is longer than the green, but they are not in the same direction. A vector multiplied by 2 should just be a vector twice as long in the same direction, no?
Is there something I don’t understand about how vectors work in Unity? How can I have the red vector be in the same direction but just be twice as long?
Thanks in advance