2017-01-18 2 views
1

안녕하세요 마법사, 난 당신이지도를있는 적을 공격 할 수있는 3D 첫번째 사람 RPG를 만들고있어3D 유니티 C#을 적은 벽을 통해 인터넷의

을 안내합니다. 현재 스크립트가 잘 작동하고 있지만 한 가지 문제가 있습니다. 플레이어가 플레이어에게 집중할 때마다 적을 벽을 통해 걷고 있습니다.

이 문제를 해결하기 위해 이미 몇 가지 시도를했지만 불행히도 결과가 없습니다. 누구든지이 문제에 대한 해결책을 알고 있습니까?

미리 감사드립니다.

goblinAttack.cs :

using UnityEngine; 
using System.Collections; 

public class goblinAttack : MonoBehaviour { 

    public Transform player; 
    static Animator anim; 

    // Use this for initialization 
    void Start() 
    { 
     anim = GetComponent<Animator>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     Vector3 direction = player.position - this.transform.position; 
     float angle = Vector3.Angle(direction,this.transform.forward); 
     if(Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30) 
     { 

      direction.y = 0; 

      this.transform.rotation = Quaternion.Slerp(this.transform.rotation, 
             Quaternion.LookRotation(direction), 0.1f); 

      anim.SetBool("isIdle",false); 
      if(direction.magnitude > 5) 
      { 
       this.transform.Translate(0,0,0.10f); 
       anim.SetBool("isRunning",true); 
       anim.SetBool("isAttacking",false); 
       anim.SetBool("isCrying",true); 
      } 
      else 
      { 
       anim.SetBool("isAttacking",true); 
       anim.SetBool("isRunning",false); 
       anim.SetBool("isCrying",false); 
      } 

     } 
     else 
     { 
      anim.SetBool("isIdle", true); 
      anim.SetBool("isRunning", false); 
      anim.SetBool("isAttacking", false); 
      anim.SetBool("isCrying",false); 

     } 

    } 
} 

NPC 설정 : IsKinematic is not affected by Forces, collisions or joints.으로 표시된 강체가 명심

settings

+1

당신이 당신의 벽 객체에 메쉬 입자 가속기가 있습니까? –

+0

플레이어에게 합법적 인 경로를 찾으려면 길 찾기를 수행해야합니다. 그렇지 않으면 고블린은 플레이어에서 직접 실행을 시도합니다. 이는 도중에 방해받을 가능성이있는 장애물을 통해 중도에 들려서 또는 달리기를 의미합니다. – Abion47

+0

예, 있습니다. 그러나 그것은 여전히 ​​벽을 통해 움직입니다. –

답변

1

강체로 Transform.Translate()를 사용하여 버그가 발생할 수도 있습니다 - 물리학 용으로 사용하려는 Rigidbody.MovePosition()을 고려하십시오. 그러나 전반적으로, 나는 적이 먼저 벽을 걸러 가지 않도록하기 위해 pathfinding을 사용하는 것이 좋습니다.

+1

IsKinematic과 관련이 없습니다. 이것은 Transform.Translate 문제입니다. MovePosition 또는 AddForce를 사용해야합니다. – Programmer

+0

알겠습니다. MovePosition으로 바꾸려면 정확히 무엇이 필요합니까? 모든 변환? –

+0

당신은'GetComponent'를 가진 Rigidbody를 가져 와서'this.transform.Translate (0,0,0.10f);를'yourRigidBody.MovePosition (transform.position + transform.forward * Time.deltaTime);으로 대체해야합니다, ' – Programmer

0

이 작업을 시도 할 수 있습니다 :

using UnityEngine; 
using System.Collections; 

public class walkTowardsPlayer : MonoBehaviour { 
List<GameObject> enemies = new List<GameObject>(); 
public GameObject player; //drag your player in inspector to this variable 
public GameObject zombie; //drag your enemy in inspector to this variable 


void Start(){ 

} 

void Update(){ 
    foreach(GameObject exampleZombie in enemies){ 
     if (zombiechar.transform.position!=player.transform.position){ 
     zombiechar.transform.position = new Vector3(zombiechar.transform.position.x, 0f, zombiechar.transform.position.z); //enemy move only at x and z axes 
     zombiechar.transform.position = Vector3.MoveTowards(zombiechar.transform.position, player.transform.position, 1.5f*Time.deltaTime); //the enemy move towards you 

     } 

      zombie = exampleZombie; 
     } 
} 

} 
+0

물론 적들은 강체 나 캐릭터 컨트롤러가 필요합니다. –

관련 문제