是否可以从另一个C#脚本运行一个C#脚本?

是否可以从另一个C#脚本运行一个C#脚本?,c#,unity3d,3d,C#,Unity3d,3d,我知道这是一个愚蠢的问题,但我就是想不通。我有两个脚本,RandomWalk.cs和Chase.cs 它们各自都可以很好地工作,但是我正在尝试获得它,以便chase.cs通过如下的if语句调用randomwalk.cs: void Update() { if (Vector3.Distance(player.position, this.transform.position) < 20) { Attack(); } else if (Vect

我知道这是一个愚蠢的问题,但我就是想不通。我有两个脚本,RandomWalk.cs和Chase.cs

它们各自都可以很好地工作,但是我正在尝试获得它,以便chase.cs通过如下的if语句调用randomwalk.cs:

void Update()
{
    if (Vector3.Distance(player.position, this.transform.position) < 20)
    {
        Attack();
    }
    else if (Vector3.Distance(player.position, this.transform.position) > 20)
    {
        //Run RandomWalk.cs here
    }
}
void Update()
{
if(向量3.距离(player.position,this.transform.position)<20)
{
攻击();
}
else if(向量3.距离(player.position,this.transform.position)>20)
{
//在这里运行RandomWalk.cs
}
}
这可能吗?以下是整个脚本供参考:

public class Chase : MonoBehaviour
{
    public Transform player;
    public float speed = 5;
    public float directionChangeInterval = 1;
    public float maxHeadingChange = 30;

    CharacterController controller;
    float heading;
    Vector3 targetRotation;

    void Update()
    {
        if (Vector3.Distance(player.position, this.transform.position) < 20)
        {
            Attack();
        }
        else if (Vector3.Distance(player.position, this.transform.position) > 20)
        {
            //Run RandomWalk.cs here            
        }
    }

    public void Attack() {
        Vector3 direction = player.position - this.transform.position;
        direction.y = 0;
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), .1f);

        if (direction.magnitude > 5) {
            this.transform.Translate(0, 0, .29f);
        }

        print("Attacking");
    }
}
公共类追逐:单一行为
{
公共转换播放器;
公共浮动速度=5;
公共浮点方向更改间隔=1;
公共浮点数maxHeadingChange=30;
字符控制器;
浮动航向;
矢量3定向;
无效更新()
{
if(向量3.距离(player.position,this.transform.position)<20)
{
攻击();
}
else if(向量3.距离(player.position,this.transform.position)>20)
{
//在这里运行RandomWalk.cs
}
}
公开无效攻击(){
Vector3方向=player.position-this.transform.position;
方向y=0;
this.transform.rotation=Quaternion.Slerp(this.transform.rotation,Quaternion.LookRotation(方向),.1f);
如果(方向大小>5){
这个.transform.Translate(0,0,29f);
}
打印(“攻击”);
}
}
编辑: 下面是我试图从if语句运行的类:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class RandomWalk : MonoBehaviour {
public float speed = 5;
public float directionChangeInterval = 1;
public float maxHeadingChange = 30;
public Transform player;

CharacterController controller;
float heading;
Vector3 targetRotation;



    public void Awake()
{
        controller = GetComponent<CharacterController>();

        // Set random initial rotation
        heading = Random.Range(0, 360);
        transform.eulerAngles = new Vector3(0, heading, 0);

        StartCoroutine(NewHeading());
    }

   public void Update()
{
        transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
        var forward = transform.TransformDirection(Vector3.forward);
        controller.SimpleMove(forward * speed);

    }

    IEnumerator NewHeading()
{
        while (true)
        {
            NewHeadingRoutine();
            yield return new WaitForSeconds(directionChangeInterval);
        }
    }

    void NewHeadingRoutine()
{
        var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
        var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
        heading = Random.Range(floor, ceil);
        targetRotation = new Vector3(0, heading, 0);
    }
}
使用UnityEngine;
使用系统集合;
[RequiredComponent(typeof(CharacterController))]
公共课堂:单一行为{
公共浮动速度=5;
公共浮点方向更改间隔=1;
公共浮点数maxHeadingChange=30;
公共转换播放器;
字符控制器;
浮动航向;
矢量3定向;
公共图书馆
{
控制器=GetComponent();
//设置随机初始旋转
航向=随机范围(0,360);
transform.eulerAngles=新矢量3(0,标题,0);
start例程(NewHeading());
}
公共无效更新()
{
transform.eulerAngles=Vector3.Slerp(transform.eulerAngles,targetRotation,Time.deltaTime*directionChangeInterval);
var forward=transform.TransformDirection(Vector3.forward);
控制器。SimpleMove(前进*速度);
}
IEnumerator NewHeading()
{
while(true)
{
NewHeadingRoutine();
返回新的WaitForSeconds(directionChangeInterval);
}
}
void NewHeadingRoutine()
{
变量地板=数学夹具(标题-最大标题更改,0,360);
变量ceil=数学夹具(航向+最大航向变化,0,360);
标题=随机范围(楼层、天花板);
targetRotation=新矢量3(0,航向,0);
}
}

我终于想起来了,只需要更改RandomWalk类以合并Chase类

[RequireComponent(typeof(CharacterController))]
public class RandomWalk : MonoBehaviour {
public float speed = 5;
public float directionChangeInterval = 1;
public float maxHeadingChange = 30;
public Transform player;

CharacterController controller;
float heading;
Vector3 targetRotation;



    public void Awake() {
        controller = GetComponent<CharacterController>();

        // Set random initial rotation
        heading = Random.Range(0, 360);
        transform.eulerAngles = new Vector3(0, heading, 0);

        StartCoroutine(NewHeading());
    }

   public void Update() {
    if (Vector3.Distance(player.position, this.transform.position) < 20)
    {
        Attack();
        print("Attacking");
    }
    else if (Vector3.Distance(player.position, this.transform.position) > 20)
    {
        idleWalk();
        print("Idly walking");
    }

}

    IEnumerator NewHeading(){
        while (true)
        {
            NewHeadingRoutine();
            yield return new WaitForSeconds(directionChangeInterval);
        }
    }

    void NewHeadingRoutine(){
        var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
        var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
        heading = Random.Range(floor, ceil);
        targetRotation = new Vector3(0, heading, 0);
    }

public void Attack() {
    Vector3 direction = player.position - this.transform.position;
    direction.y = 0;
    this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                        Quaternion.LookRotation(direction), .1f);

    if (direction.magnitude > 5) {
        this.transform.Translate(0, 0, .29f);
    }

    print("Attacking");
}

public void idleWalk() {
    transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
    var forward = transform.TransformDirection(Vector3.forward);
    controller.SimpleMove(forward * speed);
}


}
[RequireComponent(typeof(CharacterController))]
公共课堂:单一行为{
公共浮动速度=5;
公共浮点方向更改间隔=1;
公共浮点数maxHeadingChange=30;
公共转换播放器;
字符控制器;
浮动航向;
矢量3定向;
公共图书馆{
控制器=GetComponent();
//设置随机初始旋转
航向=随机范围(0,360);
transform.eulerAngles=新矢量3(0,标题,0);
start例程(NewHeading());
}
公共无效更新(){
if(向量3.距离(player.position,this.transform.position)<20)
{
攻击();
打印(“攻击”);
}
else if(向量3.距离(player.position,this.transform.position)>20)
{
idleWalk();
印刷品(“闲逛”);
}
}
IEnumerator NewHeading(){
while(true)
{
NewHeadingRoutine();
返回新的WaitForSeconds(directionChangeInterval);
}
}
void NewHeadingRoutine(){
变量地板=数学夹具(标题-最大标题更改,0,360);
变量ceil=数学夹具(航向+最大航向变化,0,360);
标题=随机范围(楼层、天花板);
targetRotation=新矢量3(0,航向,0);
}
公开无效攻击(){
Vector3方向=player.position-this.transform.position;
方向y=0;
this.transform.rotation=Quaternion.Slerp(this.transform.rotation,
四元数旋转(方向),.1f);
如果(方向大小>5){
这个.transform.Translate(0,0,29f);
}
打印(“攻击”);
}
public void idleWalk(){
transform.eulerAngles=Vector3.Slerp(transform.eulerAngles,targetRotation,Time.deltaTime*directionChangeInterval);
var forward=transform.TransformDirection(Vector3.forward);
控制器。SimpleMove(前进*速度);
}
}

调用
randomwalk.cs
或从
randomwalk.cs
调用函数?调用randomwalk.cs您不能调用类。可以在类内调用函数。也许您正在寻找一种方法来创建新的
randomwalk
?我不能确切地告诉你你在做什么,但是你应该解释为什么你需要“调用一个类”。感谢你的建议,我添加了randomwalk类来显示我试图运行的内容。你只添加了一个
randomwalk
类。你说的跑步是什么意思?当你说“跑”时,你预计会发生什么?