C# AI不跟随航路点

C# AI不跟随航路点,c#,unity3d,C#,Unity3d,我正在做一个游戏,要求敌人跟随Wayents。现在的问题是,它似乎不想进入“慢速”状态,因此不想进入下一个航路点。我认为这可能与“waitforseconds”有关,但我不太确定这是否是问题所在。敌人也无缘无故地向上移动。我使用的脚本是从Javascript转换而来的,因此问题也可能源于此 我知道这是一个巨大的问题墙,但我已经尝试了几天(可能甚至一周)来解决这个问题,但没有任何成功。任何帮助都将不胜感激 using UnityEngine; using System.Collections;

我正在做一个游戏,要求敌人跟随Wayents。现在的问题是,它似乎不想进入“慢速”状态,因此不想进入下一个航路点。我认为这可能与“waitforseconds”有关,但我不太确定这是否是问题所在。敌人也无缘无故地向上移动。我使用的脚本是从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.
    }
}
使用UnityEngine;
使用系统集合;
公共类Bloop:单一行为{
//从UnityScript转换为C#athttp://www.M2H.nl/files/js_to_c.php -迈克·赫加登
//测试代码!您通常需要更改一些位//这是一个非常简单的航路点系统。
//对于需要解释每一行的人(像我一样!),每一行都会被尽可能详细地解释。
//
//作为对没有经验的人(像我现在这样!)的补充,您可以删除任何变量上的“public”一词,以便在用于调试的检查器中查看它。
//
//我确信这是有问题的,但作为一个演示,它似乎工作得很好。
//
//步骤:
//1.将此脚本附加到具有RidgidBody和碰撞器的游戏对象。
//2.将“航路点”中的“大小”变量更改为要使用的航路点数。
//3.将航路点对象放置在空的变量插槽上。
//4.确保所有航路点对象都有碰撞器(球体碰撞器是最好的)。
//5.单击航路点对象上“is触发器”至“On”的复选框,使其成为触发器。
//6.为您的航路点设置大小(球体碰撞器的半径)或缩放。
//7.玩得开心!尝试改变变量以获得不同的速度等等。
//
//免责声明:
//外部值将开始把事情搞砸。
//也许比我更有经验的人知道如何改进它。
//如果我的任何评论不正确,请纠正我。
public float accel=0.8f;//这是调用函数“Accell()”后的加速率。值越高,对象将在更短的时间内达到“speedLimit”。
公共浮点惯性=0.9f;//这是调用函数“Slow()”后保留的速度量。较低的值会导致更快的停止。值“1.0f”永远不会停止。高于“1.0f”的值会加速。
public float speedLimit=10.0f;//这是允许对象移动的速度。
public float minSpeed=1.0f;//这是告诉函数“Slow()”何时停止移动对象的速度。
//公共浮动旋转=1.0f;
公共四元数旋转;
public float stopTime=1.0f;//这是在激活函数“Accell()”以再次启动脚本之前,在“Slow()”内暂停的时间。
//这个变量“currentSpeed”是处理速度的主要参与者。
//“currentSpeed”被变量“accel”倍增,以在函数“accell()”内加速。
//同样,“currentSpeed”乘以变量“惯性”,以降低函数“slow()”中的速度。
公共浮动电流速度=0.0f;
//变量“functionState”控制哪个函数“Accell()”或“Slow()”处于活动状态。“0”是函数“Accell()”,而“1”是函数“Slow()”。
公共浮点函数状态=0.0f;
//接下来的两个变量用于确保函数“Accell()”运行时,函数“Slow()”无法运行(反之亦然)。
公立学校;
公共厕所;
//此变量将存储“活动”目标对象(要移动到的航路点)。
公共转换航路点;
//这是对象旋转到面对活动航路点的速度。
公共浮子旋转阻尼=6.0f;
//如果这是假的,物体将立即朝着航路点旋转。如果是真的,你会得到斯穆特旋转宝贝!
布尔平滑旋转=真;
//此变量是一个数组。[]<如果您不知道,它是一个数组容器。它包含您在inspector中指定的所有航路点对象。
公共转换[]航路点;
//此变量跟踪前面提到的阵列变量“waypoints”中哪个航路点对象当前处于活动状态。
私有int WPindexPointer;
//功能!他们做所有的工作。
//您可以使用此处的内置函数:[url]http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html[/url]
//或者您可以声明自己的!函数“Accell()”是我声明的函数。
//您需要声明自己的函数,因为在“Update()”中只有某些东西不起作用http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html[/url]
//函数“Start()”在调用其他函数之前被调用,但只调用了一次。
无效开始(){
functionState=0;//脚本启动时,将“0”或函数Accell()设置为活动状态。
}
//函数“Update()”每帧调用一次。如果使用过度,它可能会变慢。
无效更新(){
if(functionState==0)//如果functionState变量当前为“0”,则运行“Accell()”。如果没有“if”,则“Accell()”将运行每一帧。
{
Accell();
}
if(functionState==1)//如果functionState变量当前为“1”,则运行“Slow()”。如果没有“if”,则“Slow()”将运行每一帧。
{
慢();
}
航路点=航路点[WPindexPointer];//使对象指向当前航路点对象。
}
//我声明了“Accell()”。
void Accell(){
如果(accelState==false)//
{                   //
accelState=true;//确保如果Accell()正在运行,则Slow()无法运行。
slowState=false//
}
//
//我从unity“SmoothLookAt”脚本中抓起了下一部分,但尝试解释更多。
如果(航路点)//如果有航路点,则执行下一个“如果”。
{
if(smoothRotation)//如果smoothRotation设置为“开”,则执行以下操作