2014-11-29 3 views
0

상승 된 지형과 바위를 걷는 게임 개체와 관련된 문제를 해결하기 위해 노력했습니다. 바위에는 충돌 장치가 있고 적 개체에는 충돌 장치와 강체가 있습니다. FPS가 일정 범위 내에서 움직일 때 적을 움직이는 대본을 첨부했습니다. 이 모든 것은 잘 작동하지만 바위와 높은 지형을 통과합니다. 지금까지 많은 제안을 시도했지만 아무것도 작동하지 않는 것 같습니다.적 개체가 개체를 통해 이동합니다.

using UnityEngine; 
using System.Collections; 

public class CreatureWalk : MonoBehaviour { 

    public int totalSecs = 0; 
    public GameObject explosion; 

    // Use this for initialization 
    void Start() { 

    } 

    // Update is called once per frame 
    void FixedUpdate() { 

     GameObject GM = GameObject.Find ("GameManager"); 

     //Get Game Manager Component on Object 
     GameManager GM_Component = GM.GetComponent<GameManager>(); 

     if (GM_Component.GameOver) { 
      animation.Play ("idle"); 

     } else if (GM_Component.FlowerHits > 30) { 
       while (totalSecs < 5000) { 
         animation.Play ("hit"); 
         totalSecs += 1; 
       } 

       //Destroy (gameObject); 

       //create explosion 
       Instantiate (explosion, transform.position, transform.rotation); 
       Invoke("Disable",3); 
       Invoke("Reenable",3); 

       Vector3 currentPosition = this.transform.position; //get current position of creature 
       transform.position = new Vector3(currentPosition.x+ Random.Range(-20.0F, 20.0F),0.2f,currentPosition.z +Random.Range(-20.0F, 20.0F)); 

       totalSecs =0; 
       GM_Component.FlowerHits =0; 

      } else { 
        animation.Play ("walk"); 
        MoveTowardsTarget(); 
      } 
    } 


    private void Disable() { 
     gameObject.SetActive(false); 
     //Debug.Log ("Disable"); 
    } 

    private void Reenable() { 
     gameObject.SetActive(true); 
     //Debug.Log ("Re-enable"); 
    } 



    //move towards a target at a set speed. 
    private void MoveTowardsTarget() { 
     //the speed, in units per second, we want to move towards the target 
     float speed = 1; 


     //move towards the fps 
     Vector3 targetPosition = new Vector3(Camera.main.transform.position.x,0,Camera.main.transform.position.z); 

     Vector3 currentPosition = this.transform.position; 

     transform.LookAt (targetPosition); 

     //first, check to see if we're close enough to the target 
     if (Vector3.Distance (currentPosition, targetPosition) < 30f) { 

      Vector3 directionOfTravel = targetPosition - currentPosition; 
      directionOfTravel.Normalize(); 

      this.transform.Translate (
      (directionOfTravel.x * speed * Time.deltaTime), 
      (directionOfTravel.y * speed * Time.deltaTime), 
      (directionOfTravel.z * speed * Time.deltaTime), 
      Space.World); 

      //this.rigidbody.MovePosition(directionOfTravel); 
      // (directionOfTravel.x * speed * Time.deltaTime), 
      // (directionOfTravel.y * speed * Time.deltaTime), 
      // (directionOfTravel.z * speed * Time.deltaTime)); 

      //this.rigidbody.AddForce(
      // (directionOfTravel.x * speed * Time.deltaTime), 
      // (directionOfTravel.y * speed * Time.deltaTime), 
      // (directionOfTravel.z * speed * Time.deltaTime),ForceMode.VelocityChange); 

      //rigidbody.AddForce(transform.forward * 10); 

       } else 
         animation.Play ("idle"); 
    } 
} 

난 당신이 주석 코드에 의해 볼 수 있지만 적의 그냥 자리에 산책으로 제안 AddForce로 이동하는 것을 시도했다.

생물에 강체가 있고 중력과 운동이 확인되었습니다.

+0

인스펙터에서 구성 요소 제목 옆에있는 확인란을 선택하여 충돌자가 실제로 활성화되어 있는지 확인하십시오. –

+0

"코드 스 니펫"편집기를 사용하지 마십시오. Javascript/HTML 코드에만 사용하십시오. 대신 {} 버튼을 사용하십시오. – LearnCocos2D

답변

0

1000 또는 10000 포스를 추가하여보십시오 ... 당신은 1의 분수를 사용하고 있습니다 ... 1의 질량을 지닌다면, 여전히 1 포스가 움직이게됩니다. 1000의 힘을 더하고 그의 질량이 1이고 여전히 움직이지 않는다면, 그의 rigidbody에서 기구학으로 설정되지 않았는지 확인하십시오.

아직 움직이지 않는다면, 훨씬 더 교활한 일이 벌어지고 있습니다. 나는 당신이 올바른 축을 올바른 방향으로 사용하고 있는지 확인하는 것이 좋습니다. 등등. 아마도 잘못 될 수없는 것들.

행운을 빌어 도움이 되었으면합니다.

0

AddForce를 사용할 때마다 방향 벡터에 원하는 속도 인 을 곱하십시오. 대상의 리지드 바디 질량. 또한 "isKinematic"체크 박스의 선택을 취소하십시오. 그 종류의 물리 엔진의 계산을 해당 객체에서 "비활성화"하기 때문입니다. 정확하기를 원하면 수동으로 대상의 강체의 속도를 설정할 수도 있습니다.

관련 문제