2014-07-16 2 views
-1

모두에게 좋은 날입니다. 적의 보스 배가 화면에 나타나서 우주선을 공격하고 때때로 내 공격을 피하려고합니다. 지금까지 나는 그에게 내 배를 공격하게했지만 hes는 화면 가장자리에 머물러 있습니다. 여기 내 코드는 다음과 같습니다.적 보스 움직임 AI

#pragma strict 

// here are public variables for the enemy ship that can be accessed in the inspector 
var health:int = 2; 
var explosion:GameObject; 
var expOrb:GameObject; 
var enemyBullet:GameObject; 
var expDrop:int = 3; 
var hitSound:AudioClip; 
var fireRate:float = 2.0; 

//heres the private variable counter to keep track of time for fire rate. 
private var counter:float = 0.0; 

function Update() { 
    //here we make counter count based on time for the fire rate 
    counter += Time.deltaTime; 

    //if the ship goes too far left, we destroy it. 
    if(transform.position.x < -12){ 
     Destroy(gameObject); 
    } 

    //here we shoot 4 bullets if the counter counts higher than the fire rate. 
    if(counter > fireRate){ 
     var custom1 = Instantiate(enemyBullet, transform.position - Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0)); 
     var custom2 = Instantiate(enemyBullet, transform.position - Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0)); 
     var custom3 = Instantiate(enemyBullet, transform.position- Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0)); 
     var custom4 = Instantiate(enemyBullet, transform.position- Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0)); 
     //to make the bullets spread, we add extra z velocity to each one to they all move on their own path. 
     custom1.rigidbody.velocity.z = 3; 
     custom2.rigidbody.velocity.z = 1; 
     custom3.rigidbody.velocity.z = -1; 
     custom4.rigidbody.velocity.z = -3; 
     counter = 0.0; 
    } 

    //end of function update 
} 

//if a bullet hits the ship, the bullets sends us the hit message to trigger this function to bring down the ships health 
function hit() { 
    health -= 1; 
    if(health != 0){ 
     if(audio.enabled == true){ 
     audio.PlayOneShot(hitSound); 
     } 
    } 
    if(health <= 0){ 
     onDeath(); 
    } 
} 

//if health is 0, then this function is triggered to spawn some orbs, spawn the explosion animation object, and destroy itself 
function onDeath() { 
    Instantiate(expOrb,transform.position,Quaternion.Euler(-90,0,0)); 
    expDrop -= 1; 
    if(expDrop <= 0){ 


     Instantiate(explosion,transform.position,Quaternion.Euler(-90,Random.Range(-180,180),0)); 
     Destroy(gameObject); 
    } 
    if(expDrop > 0){ 
     onDeath(); 
    } 
} 

어떻게 이동 측면을 추가합니까? 개체를 이동하는 방법에는 여러 가지가 있습니다

+0

이미 시도한 것은 무엇입니까? 왜 작동하지 않았습니까? 이 질문을하기로 결정하기 전에 검토 한 문서는 무엇입니까? 또한 코드 예제를 조금씩 요약하여 어디서 무엇을하고 싶습니까? –

답변

0

, 당신은 시도 할 수 있습니다 :

  1. Transform.translate (http://docs.unity3d.com/ScriptReference/Transform.Translate.html를)

  2. 가에 힘을 추가 transform.position (http://answers.unity3d.com/questions/188998/transformposition.html)

  3. 수정 (http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html를)

  4. ma 나도 아직 알지 못했을 다른 방법들.

이런 종류의 게임에서 보스의 경우 원하는 동작은 무작위로 움직이거나 플레이어를 따라하는 것입니다. 둘 다 성취하기가 어렵지 않습니다.

  • 랜덤 운동 : 단지 Random.Range(-1, 1) * bossSpeed * time.deltaTime을하고 보스 'X 위치에 적용합니다.
  • 플레이어 다음 : 플레이어의 x 위치를 얻은 다음 보스를 위치로 조정합니다.

그러면 사장님이 가끔 플레이어의 총알을 피하는 방법은 무엇입니까? 플레이어의 탄환이 앞에 놓인 충돌자를 추가하여 플레이어의 탄환이 들어오는 지 파악할 수 있습니다. 플레이어의 총알이 충돌하는 경우 다른 번호 (예 : 0에서 1)를 무작위로 선택합니다. 그 후에 난수가 1이면 상사를 멀리 옮기고 난수가 0 일 때 상사를 움직이지 않아야합니다.

+0

왜 너희들은 질문을 뺀거야? 그건 무례한거야. 이제 더 이상 질문 할 수 없습니다. – xhenier