AI不遵循航点

时间:2015-01-04 14:28:00

标签: c# unity3d

我正在制作一款需要敌人遵循方法的游戏。现在的问题是它似乎不想进入“慢”的状态。国家,因此不会去下一个航路点。我认为这可能与“等待时间”有关。但我不太确定这是不是问题所在。敌人也没有任何明确的理由向上移动。我使用的脚本是从Javascript转换而来的,所以问题可能源于此问题。

我知道这是一个很大的问题,但我已经试图解决这个问题好几天(甚至一周)而没有任何成功。任何帮助将不胜感激。

using UnityEngine;
using System.Collections;

public class Bloop : MonoBehaviour {

// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
// Do test the code! You usually need to change a few small bits.       //   This is a very simple waypoint system.
//   Each bit is explained in as much detail as possible for people (like me!) who need every single line explained.
//
//   As a side note to the inexperienced (like me at the moment!), you can delete the word "public" on any variable to see it in the inspector for debugging.
//
//   I am sure there are issues with this as is, but it seems to work pretty well as a demonstration.
//
//STEPS:
//1. Attach this script to a GameObject with a RidgidBody and a Collider.
//2. Change the "Size" variable in "Waypoints" to the number of waypoints you want to use.
//3. Drop your waypoint objects on to the empty variable slots.
//4. Make sure all your waypoint objects have colliders. (Sphere Collider is best IMO).
//5. Click the checkbox for "is Trigger" to "On" on the waypoint objects to make them triggers.
//6. Set the Size (radius for sphere collider) or just Scale for your waypoints.
//7. Have fun! Try changing variables to get different speeds and such.
//
//   Disclaimer:
//   Extreeme values will start to mess things up.
//   Maybe someone more experienced than me knows how to improve it.
//   Please correct me if any of my comments are incorrect.




public float accel=0.8f; //This is the rate of accelleration after the function "Accell()" is called. Higher values will cause the object to reach the "speedLimit" in less time.

public float inertia=0.9f; //This is the the amount of velocity retained after the function "Slow()" is called. Lower values cause quicker stops. A value of "1.0f" will never stop. Values above "1.0f" will speed up.

public float speedLimit= 10.0f; //This is as fast the object is allowed to go.

public float minSpeed=1.0f; //This is the speed that tells the functon "Slow()" when to stop moving the object.
//public float rotation=1.0f;
public Quaternion rotation;

public float stopTime=1.0f; //This is how long to pause inside "Slow()" before activating the function "Accell()" to start the script again.

//This variable "currentSpeed" is the major player for dealing with velocity.
//The "currentSpeed" is mutiplied by the variable "accel" to speed up inside the function "accell()".
//Again, The "currentSpeed" is multiplied by the variable "inertia" to slow things down inside the function "Slow()".
public float currentSpeed=0.0f;

//The variable "functionState" controlls which function, "Accell()" or "Slow()", is active. "0" is function "Accell()" and "1" is function "Slow()".
public float functionState=0.0f;

//The next two variables are used to make sure that while the function "Accell()" is running, the function "Slow()" can not run (as well as the reverse).
public bool  accelState;
public bool  slowState;

//This variable will store the "active" target object (the waypoint to move to).
public Transform waypoint;

//This is the speed the object will rotate to face the active Waypoint.
public float rotationDamping=6.0f;

//If this is false, the object will rotate instantly toward the Waypoint. If true, you get smoooooth rotation baby!
bool smoothRotation= true;

//This variable is an array. []< that is an array container if you didnt know. It holds all the Waypoint Objects that you assign in the inspector.
public Transform[] waypoints;

//This variable keeps track of which Waypoint Object, in the previously mentioned array variable "waypoints", is currently active.
private int WPindexPointer;





//Functions! They do all the work.
//You can use the built in functions found here: [url]http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html[/url]
//Or you can declare your own! The function "Accell()" is one I declared.
//You will want to declare your own functions because theres just certain things that wont work in "Update()". Things like Coroutines: [url]http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html[/url]


//The function "Start()" is called just before anything else but only one time.
void  Start (){
    functionState = 0; //When the script starts set "0" or function Accell() to be active.

}




//The function "Update()" is called every frame. It can get slow if overused.
void  Update (){
    if (functionState == 0) //If functionState variable is currently "0" then run "Accell()". Withouth the "if", "Accell()" would run every frame.
    {
        Accell ();
    }
    if (functionState == 1) //If functionState variable is currently "1" then run "Slow()". Withouth the "if", "Slow()" would run every frame.
    {
        Slow ();
    }
    waypoint = waypoints[WPindexPointer]; //Keep the object pointed toward the current Waypoint object.
}




//I declared "Accell()".
void  Accell (){
    if (accelState == false) //
    {                   //
        accelState = true;    //Make sure that if Accell() is running, Slow() can not run.
        slowState = false;    //
    }
    //
    //I grabbed this next part from the unity "SmoothLookAt" script but try to explain more.
    if (waypoint) //If there is a waypoint do the next "if".
    {
        if (smoothRotation) //If smoothRotation is set to "On", do the rotation over time with nice ease in and ease out motion.
        {
            //Look at the active waypoint.
            Quaternion rotation= Quaternion.Euler(waypoint.position - transform.position);
            //Make the rotation nice and smooth.
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
        }
    }
    //Now do the accelleration toward the active waypoint untill the "speedLimit" is reached
    currentSpeed = currentSpeed + accel * accel;
    transform.Translate (0,0,Time.deltaTime * currentSpeed);

    //When the "speedlimit" is reached or exceeded ...
    if (currentSpeed >= speedLimit)
    {
        // ... turn off accelleration and set "currentSpeed" to be exactly the "speedLimit". Without this, the "currentSpeed will be slightly above "speedLimit"
        currentSpeed = speedLimit;
    }
}




//The function "OnTriggerEnter" is called when a collision happens.
void  OnTriggerEnter (){
    functionState = 1; //When the GameObject collides with the waypoint's collider, activate "Slow()" by setting "functionState" to "1".
    WPindexPointer++;  //When the GameObject collides with the waypoint's collider, change the active waypoint to the next one in the array variable "waypoints".

    //When the array variable reaches the end of the list ...
    if (WPindexPointer >= waypoints.Length)
    {
        WPindexPointer = 0; // ... reset the active waypoint to the first object in the array variable "waypoints" and start from the beginning.
    }
}




//I declared "Slow()".
void  Slow (){
    if (slowState == false) //
    {                  //
        accelState = false; //Make sure that if Slow() is running, Accell() can not run.
        slowState = true;   //
    }                  //

    //Begin to do the slow down (or speed up if inertia is set above "1.0f" in the inspector).
    currentSpeed = currentSpeed * inertia;
    transform.Translate (0,0,Time.deltaTime * currentSpeed);

    //When the "minSpeed" is reached or exceeded ...
    if (currentSpeed <= minSpeed)
    {
        currentSpeed = 0.0f; // ... Stop the movement by setting "currentSpeed to Zero.
                //WaitForSeconds (stopTime); //Wait for the amount of time set in "stopTime" before moving to next waypoint.
        functionState = 0; //Activate the function "Accell()" to move to next waypoint.
    }
}

}

0 个答案:

没有答案
相关问题