2016-08-10 4 views
0

나는 이것을 가장 쉬운 방법으로 설명하려고 노력할 것입니다. 나는 적 플레이어를 위해 약간의 인공 지능을 만들고 있습니다. AI가 레이 캐스트가 플레이어에게 닿으면 AI가 뒤로 이동하기를 원하지만 심각한 문제가 있습니다. [코드 아래에 설명 할 것]Unity 업데이트 기능을 일시적으로 무시하는 방법 [C#]

코드;

using UnityEngine; 
using System.Collections; 

public class NewEnemyAI : MonoBehaviour 
{ 
    public GameObject enemy; 
    public Transform target; 
    public float movementSpeed = 2f; 

    private CharacterController enemyCharacterController; 
    private bool enemyHit; 
    private Vector3 startPos; 
    private Vector3 endPos; 
    private float distance = 7f; 
    private float lerpTime = 0.3f; 
    private float currentLerpTime = 0f; 
    private bool avoid; 
    private bool updateOn = true; 
    private bool hitPlayer = false; 


    // Use this for initialization 
    void Start() 
    { 
     avoid = false; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     //raycasting 
     if (Physics.Raycast(transform.position, transform.forward, 2)) 
     { 
      startPos = enemy.transform.position; 
      endPos = enemy.transform.position + Vector3.back * distance; 
      avoid = true; 
     } 
     else 
     { 
      avoid = false; 
     } 

     if (avoid == true) 
     { 
      currentLerpTime += Time.deltaTime; 
      if (currentLerpTime >= lerpTime) 
      { 
       currentLerpTime = lerpTime; 
      } 

      float perc = currentLerpTime/lerpTime; 
      enemy.transform.position = Vector3.Lerp(startPos, endPos, perc); 

      avoid = false; 
     } 

     if (avoid == false) 
     { 
      currentLerpTime = 0; 
      transform.LookAt(target); 
      transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed); 
     } 
    } 
} 

적의 레이 캐스트 플레이어 안타

는, 그에 해당하는 경우, 그것은 플레이어의 움직임을 다시 만드는 true로 부울을 설정합니다. 그러나 플레이어가 뒤로 움직이기 때문에 레이 캐스팅이 플레이어에게 더 이상 부딪치지 않으며 코드에서 코드로 점프하기 때문에이 전체 프로세스가 다시 시작되고 적의 고무줄이 생깁니다. 코드가 광선 캐스트 if 문을 무시하고 달리 말하지 않는 한 영구적으로 bool 피를 true로 설정하는 방법이 있습니까? 감사! [저는 C#에 비교적 익숙합니다. 제 코드를 너무 비판하지 마십시오.]

답변

2

쉬운 수정 :

if (Physics.Raycast(transform.position, transform.forward, 2) && timePassed(timestamp)) 

을 그리고 당신은이 예제에 timePassed 기능 내놓고을 구현할 수 있습니다 : if 문에 타이머를 추가 그러나 https://docs.unity3d.com/ScriptReference/Time-time.html

, 당신이해야 : 소스

using UnityEngine; 
using System.Collections; 

public class ExampleClass : MonoBehaviour { 
    public GameObject projectile; 
    public float fireRate = 0.5F; 
    private float nextFire = 0.0F; 
    void Update() { 
     if (Input.GetButton("Fire1") && Time.time > nextFire) { 
      nextFire = Time.time + fireRate; 
      GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject; 
     } 
    } 
} 

객체 지향 프로그래밍의 규칙을 염두에두고 코드를 리팩토링하는 것을 고려하십시오. 좋은 소스를 통해 학습하면 결국 시간이 많이 걸리고 실수를하게됩니다. 침대에서 코드를 디자인하고 패치를 시도했습니다.

책상에서 책상에 이르기까지 좋은 C# 튜토리얼을 읽고 게임을 통해 무엇을 배웠는지 연습하는 것이 좋습니다.

+0

"쿨 다운"개념에 동의합니다. 그것은 불필요한 충돌 검사 및 올바른 상황에서 실패 할 수있는 다른 것들을 피할 수 있습니다. – Krythic

관련 문제