2017-03-11 3 views
0

私はこれが愚かな質問であることを知っていますが、私はそれの周りに私の頭を包むことはできません。私は2つのスクリプト、RandomWalk.csChase.csを持っています。彼らの両方が個別に正常に動作別のC#スクリプトからC#スクリプトを実行することはできますか?

、しかし私はchase.csが通過randomwalk.csを呼び出すようにそれを取得しようとしている。このようなif文:

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); 
    } 
} 
+0

'randomwalk.cs'を呼び出すか' randomwalk.cs'から関数を呼び出しますか? – Programmer

+0

randomwalk.csを呼び出します。 – CEamonn

+2

クラスを呼び出すことはできません。クラスの中で関数を呼び出すことができます。おそらく、あなたは 'randomwalk'の新しいインスタンスを作成する方法を探していますか?あなたがしていることを正確に伝えることはできませんが、なぜあなたは "クラスを呼び出す"必要があるのか​​を説明する必要があります。 – Programmer

答えて

0

は最後だけに必要な、それのまわりで私の頭を包んChaseクラスを組み込むようにRandomWalkクラスを変更する

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


} 
関連する問題