-1

The only problem i face now is that in the Inspector when changing the Patrol Speed value it's not effecting at all the speed of the character walk.

I did the character to walk in the window > Animator and there i created a new Empty State called it Walk and set the Motion to HumanoidWalk then i set the Walk state to Set as Layer Default State

So now my character is walking all the time and with the script i tell him to walk between the waypoints.

Thep roblem is how to change the walking speed ?

#pragma strict

// The list of Waypoint you want the enemy to traverse
public var waypoint : Transform[];  

// The walking speed between Waypoints    
public var patrolSpeed : float = 6;  

// Do you want to keep repeating the Waypoints    
public var loop : boolean = true;      

// How slowly to turn
public var dampingLook = 4;

// How long to pause at a Waypoint= 0;          
public var pauseDuration : float;

private var curTime : float;
private var currentWaypoint : int = 0;
public var character : CharacterController;

function Start(){

    //character = GetComponent(CharacterController);
}

function LateUpdate(){

    if(currentWaypoint < waypoint.length){
       patrol();
       }else{    
    if(loop){
       currentWaypoint=0;
        }
    }
}

function patrol(){

    var nextWayPoint : Vector3 = waypoint[currentWaypoint].position;

    // Keep waypoint at character's height
    nextWayPoint.y = transform.position.y;

    // Get the direction we need to move to
    // reach the next waypoint
    var moveDirection : Vector3 = nextWayPoint - transform.position;


    if(moveDirection.magnitude < 1.5){
        Debug.Log("enemy is close to nextwaypoint");


    // This section of code is called only whenever the enemy
    // is very close to the new waypoint
    // so it is called once after 4-5 seconds.

       if (curTime == 0)
           // Pause over the Waypoint
           curTime = Time.time;

           if ((Time.time - curTime) >= pauseDuration){
               Debug.Log("increasing waypoint");

                currentWaypoint++;
                curTime = 0;
           }
    }
    else
    {    
    Debug.Log("reaching in rotation " + moveDirection.magnitude);
       // This code gets called every time update is called
       // while the enemy if moving from point 1 to point 2.
       // so it gets called 100's of times in a few seconds  

       // Now we need to do two things
       // 1) Start rotating in the desired direction
       // 2) Start moving in the desired direction

       // 1) Let' calculate rotation need to look at waypoint
       // by simply comparing the desired waypoint & current transform
       var rotation = Quaternion.LookRotation(nextWayPoint - transform.position);

       // A slerp function allow us to slowly start rotating
       // towards our next waypoint
       transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
                Time.deltaTime * dampingLook);

       // 2) Now also let's start moving towards our waypoint
       character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
    }  
}
TheLost Lostit
  • 505
  • 6
  • 28
  • I tried to change the line: public var patrolSpeed : float = 6; to public var patrolSpeed : float = 20; but no change same speed. I used a break point and it does getting to the line: character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime); but the speed never change. – TheLost Lostit Jul 25 '16 at 23:56

1 Answers1

0

If it is a public variable it may not be represented in the editor as the change that you have made inside the script, try setting the patrol speed inside the editor.

  • 1
    It's working now once i'm changing the Animator Walk speed. Added new Animator variable and in the Update function i set the Animation variable speed property to the patrolSpeed. Now it's working. – TheLost Lostit Jul 26 '16 at 02:04