상승 된 지형과 바위를 걷는 게임 개체와 관련된 문제를 해결하기 위해 노력했습니다. 바위에는 충돌 장치가 있고 적 개체에는 충돌 장치와 강체가 있습니다. 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로 이동하는 것을 시도했다.
생물에 강체가 있고 중력과 운동이 확인되었습니다.
인스펙터에서 구성 요소 제목 옆에있는 확인란을 선택하여 충돌자가 실제로 활성화되어 있는지 확인하십시오. –
"코드 스 니펫"편집기를 사용하지 마십시오. Javascript/HTML 코드에만 사용하십시오. 대신 {} 버튼을 사용하십시오. – LearnCocos2D