Search Unity

Vector3.MoveTowards() doesn't work properly

Discussion in 'Scripting' started by The-Game-Master, Aug 29, 2014.

  1. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    I'm making a zombie AI script, and when I drag-and-drop this code onto the enemy, and drag the "Player" Transform onto the transform box, the console ends up saying
    --------------------
    something like this:
    transform.position assign attempt for 'Zombie' is not valid. Input position is { -125138735291163730000000000000000000000.000000, 319500158179833610000000000000000000000.000000, -Infinity }.
    UnityEngine.Transform:Translate(Vector3)
    Zombie:Update() (at Assets/AI Scripts/Zombie.cs:13)
    --------------------
    Here is my code:
    1. using UnityEngine;
    2. using System.Collections;

    3. public class Zombie : MonoBehaviour {
    4. public Transform Player;

    5. // Use this for initialization
    6. void Start () {
    7. }
    8. // Update is called once per frame
    9. void Update () {
    10. transform.Translate (Vector3.MoveTowards (transform.position, Player.transform.position, 1));
    11. Debug.Log (transform.position);
    12. }
    13. }
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Try;

    Code (CSharp):
    1. transform.position = (Vector3.MoveTowards (transform.position, Player.transform.position, 1));
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Translate takes a translation. You have given it a position
    try transform.position = Vector3.MoveTowards...