我正在youtube上按照旧指南使用googleVR https://www.youtube.com/watch?v=kIATVY0sXuU&t=751s进行FPS。 创建者发布了这两个脚本。
using UnityEngine;
using System.Collections;
public class zombieScript : MonoBehaviour {
//declare the transform of our goal (where the navmesh agent will move towards) and our navmesh agent (in this case our zombie)
private Transform goal;
private NavMeshAgent agent;
// Use this for initialization
void Start () {
//create references
goal = Camera.main.transform;
agent = GetComponent<NavMeshAgent>();
//set the navmesh agent's desination equal to the main camera's position (our first person character)
agent.destination = goal.position;
//start the walking animation
GetComponent<Animation>().Play ("walk");
}
//for this to work both need colliders, one must have rigid body, and the zombie must have is trigger checked.
void OnTriggerEnter (Collider col)
{
//first disable the zombie's collider so multiple collisions cannot occur
GetComponent<CapsuleCollider>().enabled = false;
//destroy the bullet
Destroy(col.gameObject);
//stop the zombie from moving forward by setting its destination to it's current position
agent.destination = gameObject.transform.position;
//stop the walking animation and play the falling back animation
GetComponent<Animation>().Stop ();
GetComponent<Animation>().Play ("back_fall");
//destroy this zombie in six seconds.
Destroy (gameObject, 6);
//instantiate a new zombie
GameObject zombie = Instantiate(Resources.Load("zombie", typeof(GameObject))) as GameObject;
//set the coordinates for a new vector 3
float randomX = UnityEngine.Random.Range (-12f,12f);
float constantY = .01f;
float randomZ = UnityEngine.Random.Range (-13f,13f);
//set the zombies position equal to these new coordinates
zombie.transform.position = new Vector3 (randomX, constantY, randomZ);
//if the zombie gets positioned less than or equal to 3 scene units away from the camera we won't be able to shoot it
//so keep repositioning the zombie until it is greater than 3 scene units away.
while (Vector3.Distance (zombie.transform.position, Camera.main.transform.position) <= 3) {
randomX = UnityEngine.Random.Range (-12f,12f);
randomZ = UnityEngine.Random.Range (-13f,13f);
zombie.transform.position = new Vector3 (randomX, constantY, randomZ);
}
}
}
using UnityEngine;
using System.Collections;
public class playerScript : MonoBehaviour {
//declare GameObjects and create isShooting boolean.
private GameObject gun;
private GameObject spawnPoint;
private bool isShooting;
// Use this for initialization
void Start () {
//only needed for IOS
Application.targetFrameRate = 60;
//create references to gun and bullet spawnPoint objects
gun = gameObject.transform.GetChild (0).gameObject;
spawnPoint = gun.transform.GetChild (0).gameObject;
//set isShooting bool to default of false
isShooting = false;
}
//Shoot function is IEnumerator so we can delay for seconds
IEnumerator Shoot() {
//set is shooting to true so we can't shoot continuosly
isShooting = true;
//instantiate the bullet
GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
//Get the bullet's rigid body component and set its position and rotation equal to that of the spawnPoint
Rigidbody rb = bullet.GetComponent<Rigidbody>();
bullet.transform.rotation = spawnPoint.transform.rotation;
bullet.transform.position = spawnPoint.transform.position;
//add force to the bullet in the direction of the spawnPoint's forward vector
rb.AddForce(spawnPoint.transform.forward * 500f);
//play the gun shot sound and gun animation
GetComponent<AudioSource>().Play ();
gun.GetComponent<Animation>().Play ();
//destroy the bullet after 1 second
Destroy (bullet, 1);
//wait for 1 second and set isShooting to false so we can shoot again
yield return new WaitForSeconds (1f);
isShooting = false;
}
// Update is called once per frame
void Update () {
//declare a new RayCastHit
RaycastHit hit;
//draw the ray for debuging purposes (will only show up in scene view)
Debug.DrawRay(spawnPoint.transform.position, spawnPoint.transform.forward, Color.green);
//cast a ray from the spawnpoint in the direction of its forward vector
if (Physics.Raycast(spawnPoint.transform.position, spawnPoint.transform.forward, out hit, 100)){
//if the raycast hits any game object where its name contains "zombie" and we aren't already shooting we will start the shooting coroutine
if (hit.collider.name.Contains("zombie")) {
if (!isShooting) {
StartCoroutine ("Shoot");
}
}
}
}
}
第一个脚本应该使用NavMeshAgent移动僵尸,并且可以运行,但是:
1)僵尸只做步行动画2秒钟
2)当我使用第二个脚本中的项目符号时,僵尸应该使用“后备”动画,但它不起作用,您有什么解决办法吗?谢谢