博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UNITY引擎下简单FPS类游戏编写脚本一览
阅读量:5086 次
发布时间:2019-06-13

本文共 4096 字,大约阅读时间需要 13 分钟。

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
(); 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
(); 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
(); 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); }}

 

转载于:https://www.cnblogs.com/PabloZeal/p/7922957.html

你可能感兴趣的文章
中文脏话识别的解决方案
查看>>
CSS之不常用但重要的样式总结
查看>>
Python编译错误总结
查看>>
URL编码与解码
查看>>
日常开发时遇到的一些坑(三)
查看>>
Eclipse 安装SVN插件
查看>>
深度学习
查看>>
TCP粘包问题及解决方案
查看>>
构建之法阅读笔记02
查看>>
添加按钮
查看>>
移动端页面开发适配 rem布局原理
查看>>
Ajax中文乱码问题解决方法(服务器端用servlet)
查看>>
会计电算化常考题目一
查看>>
阿里云服务器CentOS6.9安装Mysql
查看>>
剑指offer系列6:数值的整数次方
查看>>
js 过滤敏感词
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
软件开发和软件测试,我该如何选择?(蜗牛学院)
查看>>
基本封装方法
查看>>
bcb ole拖拽功能的实现
查看>>