UNITY引擎下简单FPS类游戏编写脚本一览
Posted 望山海
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UNITY引擎下简单FPS类游戏编写脚本一览相关的知识,希望对你有一定的参考价值。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spin : MonoBehaviour { public float speed = 3.0f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.Rotate (0, speed, 0, Space.World); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MouseLook : MonoBehaviour { public enum RotationAxes{ MouseXAndY = 0, MouseX = 1, MouseY = 2, } public RotationAxes axes = RotationAxes.MouseX; public float sensitivityHor = 9.0f; public float sensivtiityVert = 9.0f; public float minimumVert = -45.0f; public float maxiimumVert = 45.0f; private float _rotationX = 0; // Use this for initialization void Start () { Rigidbody body = GetComponent<Rigidbody> (); if(body != null){ body.freezeRotation = true; } } // Update is called once per frame void Update () { if (axes == RotationAxes.MouseX){ transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityHor, 0); } else if (axes == RotationAxes.MouseY){ _rotationX -= Input.GetAxis ( "Mouse Y") * sensivtiityVert; _rotationX = Mathf.Clamp (_rotationX, minimumVert, maxiimumVert); float rotationY = transform.localEulerAngles.y; transform.localEulerAngles = new Vector3 ( _rotationX, rotationY, 0); } else { _rotationX -= Input.GetAxis ( "Mouse Y") * sensivtiityVert; _rotationX = Mathf.Clamp (_rotationX, minimumVert, maxiimumVert); float delta = Input.GetAxis ("Mouse X") * sensitivityHor; float rotatuinY = transform.localEulerAngles.y + delta; transform.localEulerAngles = new Vector3 (_rotationX, rotatuinY, 0); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FPSinput : MonoBehaviour { public float speed = 6.0f; private CharacterController _characterController; public float gravity = -9.8f; // Use this for initialization void Start () { _characterController = GetComponent< CharacterController> (); } // Update is called once per frame void Update () { float deltaX = Input.GetAxis ("Horizontal") * speed; float deltaZ = Input.GetAxis ("Vertical") * speed; Vector3 movement = new Vector3 (deltaX, 0, deltaZ); movement = Vector3.ClampMagnitude (movement, speed); movement.y = gravity; movement *= Time.deltaTime; movement = transform.TransformDirection (movement); _characterController.Move (movement); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RayShooter : MonoBehaviour { private Camera _camera; // Use this for initialization void Start () { _camera = GetComponent<Camera> (); Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } void OnGUI(){ int size = 12; float posX = _camera.pixelWidth / 2 - size / 4; float posY = _camera.pixelHeight / 2 - size / 2; GUI.Label (new Rect (posX, posY, size, size), "*"); } // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)){ Vector3 point = new Vector3 (_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0); Ray ray = _camera.ScreenPointToRay (point); RaycastHit hit; if(Physics.Raycast(ray, out hit)){ GameObject hitObject = hit.transform.gameObject; ReactiveTarget target = hitObject.GetComponent<ReactiveTarget> (); if(target != null){ target.ReactToHit (); }else{ StartCoroutine (SphereIndicator (hit.point)); } } } } private IEnumerator SphereIndicator(Vector3 pos){ GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere); sphere.transform.position = pos; yield return new WaitForSeconds (1); Destroy (sphere); } }
以上是关于UNITY引擎下简单FPS类游戏编写脚本一览的主要内容,如果未能解决你的问题,请参考以下文章