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

时间:2017-03-11 17:25:17

标签: 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 (Vector3.Distance(player.position, this.transform.position) > 20)
    {
        //Run RandomWalk.cs here
    }
}

这有可能吗?以下是整个脚本供参考:

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");
    }
}

编辑: 这是我尝试从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);
    }
}

1 个答案:

答案 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);
}


}