Movetowards Not Working

Hey guys this part does not work to see what i mean test this on a game object and create a second gameobject tagged by Enemy You need also a rigidbody no Character Controller The fist game object with this script attached represents a space ship attacking the Enemy Space ship the rotating part works but the But the move towards Is not Working??? Can you hel me fix this?

function MoveTowards (position : Vector3) {
        var direction = position - transform.position;
        direction.y = 0;
        if (direction.magnitude < 5) {
           SendMessage("SetSpeed", 10.0);
           return;
        }
        // Move the character
           transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
           GetComponent (Rigidbody).SimpleMove(direction);
    }

----------------------------------------------------------
Origional Script


    var speed = 3.0;
    var rotationSpeed = 5.0;
    var shootRange = 15.0;
    var shootAngle = 4.0;
    var attackRange = 300.0;
    var dontComeCloserRange = 20;
    var pickNextWaypointDistance = 2.0;
    var delayShootTime = 0.35;
    var target : Transform;
     
     
    private var lastShot = -10.0;
     
    function Start () {
        if (target == null && GameObject.FindWithTag("Enemy"))
           target = GameObject.FindWithTag("Enemy").transform;
           Patrol();
     
    }
     
    function Patrol () {
        var curWayPoint = AutoWayPoint.FindClosest(transform.position);
        while (true) {
           var waypointPosition = curWayPoint.transform.position;
           // Are we close to a waypoint? -> pick the next one!
           if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
             curWayPoint = PickNextWaypoint (curWayPoint);
     
           // Attack the Enemy and wait until
           // - Enemy is killed
           // - Enemy is out of sight       
           if (CanSeeTarget ())
             yield StartCoroutine("AttackEnemy");
     
           // Move towards our target
           MoveTowards(waypointPosition);
     
           yield;
        }
    }
     
    function Update () {
        if (target == null && GameObject.FindWithTag("Enemy"))
           target = GameObject.FindWithTag("Enemy").transform;
        if (target == null)
           return;
           Patrol();
     
        if (!CanSeeTarget ())
           return;
           
    	
    	// Rotate towards target	
    	var targetPoint = target.position;
    	var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
     
    }
     
    function CanSeeTarget () : boolean
    {
        if (Vector3.Distance(transform.position, target.position) > attackRange)
           return false;
     
        var hit : RaycastHit;
        if (Physics.Linecast (transform.position, target.position, hit))
           return hit.transform == target;
     
        return false;
    }
     
    function AttackEnemy () {
        var lastVisibleEnemyPosition = target.position;
        while (true) {
           if (CanSeeTarget ()) {
             // Target is dead - stop hunting
             if (target == null)
              return;
     
             // Target is too far away - give up  
             var distance = Vector3.Distance(transform.position, target.position);
             if (distance > shootRange * 3)
              return;
     
             lastVisibleEnemyPosition = target.position;
             if (distance > dontComeCloserRange)
              MoveTowards (lastVisibleEnemyPosition);
             else
              Patrol ();
     
             var forward = transform.TransformDirection(Vector3.forward);
             var targetDirection = lastVisibleEnemyPosition - transform.position;
             targetDirection.y = 0;
     
             var angle = Vector3.Angle(targetDirection, forward);
     
             // Start shooting if close and play is in sight
             if (distance < shootRange && angle < shootAngle)
              yield StartCoroutine("Shoot");
           } else {
             yield StartCoroutine("SearchEnemy", lastVisibleEnemyPosition);
             // Enemy not visible anymore - stop attacking
             if (!CanSeeTarget ())
              return;
           }
     
           yield;
        }
    }
     
    function SearchEnemy (position : Vector3) {
        // Run towards the Enemy but after 3 seconds timeout and go back to Patroling
        var timeout = 3.0;
        while (timeout > 0.0) {
           MoveTowards(position);
     
           // We found the Enemy
           if (CanSeeTarget ())
             return;
     
           timeout -= Time.deltaTime;
           yield;
        }
    }
     
    function MoveTowards (position : Vector3) {
        var direction = position - transform.position;
        direction.y = 0;
        if (direction.magnitude < 5) {
           SendMessage("SetSpeed", 10.0);
           return;
        }
        // Move the character
           transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    	   GetComponent (Rigidbody).SimpleMove(direction);
    }
     
    function PickNextWaypoint (currentWaypoint : AutoWayPoint) {
        // We want to find the waypoint where the character has to turn the least
     
        // The direction in which we are walking
        var forward = transform.TransformDirection(Vector3.forward);
     
        // The closer two vectors, the larger the dot product will be.
        var best = currentWaypoint;
        var bestDot = -10.0;
        for (var cur : AutoWayPoint in currentWaypoint.connected) {
           var direction = Vector3.Normalize(cur.transform.position - transform.position);
           var dot = Vector3.Dot(direction, forward);
           if (dot > bestDot && cur != currentWaypoint) {
             bestDot = dot;
             best = cur;
           }
        }
     
        return best;
    }
    // Make sure there is always a character controller
    @script RequireComponent (Rigidbody)

Hi, you can use CharacterController if you want. (You can send fake inputs). I do that to make my collisions and movement smooth.

But here is a script to rotate to position and Translating to it. (might not feel good for rigidbody collisions).

var object : GameObject;

var steerVector : Vector3 = transform.InverseTransformPoint(Vector3(object.transform.position.x, transform.position.y, object.transform.position.z));

var newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);
		dir = steerVector.x / steerVector.magnitude;
		
		transform.Rotate(0,dir * Time.deltaTime * 200,0);

if(dir < 0.3 || dir > -0.3){

transform.Translate (new Vector3(0,0, 200 * Time.deltaTime));

}

I hope you find your solution. :slight_smile:

It is unclear how you want this to behave. To start with, put a ‘#pragma strict’ at the top of the file. When you do you will find that your code won’t compile. The problem is this line:

 GetComponent (Rigidbody).SimpleMove(direction);   

…and the issue is that a Rigidbody does not have a ‘SimpleMove()’ function. If you remove this line, I think your code will work, but how it is working may not be what you want. In particular this line does the moving:

  transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

This line (and therefore MoveTowards()) needs to be called each frame. So if this line is called each frame, and ‘speed’ has a value other than 0, this code will move the object. But it is moving it through direct manipulation of the transform. If you collisions to work, this is a bit dicey. That is, directly changing a position in the transform teleports the object from one position to another, so it can result in missing out on collisions. So you can changing this one line to the following two lines may be what you are looking for:

var pos =  Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
rigidbody.MovePosition(pos);

You could also change the code so that you use through the Rigidbody instead of using the Vector3.MoveTowards() function. That is directly set the Rigidbody.velocity or use Rigidbody.AddForce().