Unity 插件往後方便使用。
插件位置圖示:
插件程式:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class FPS : MonoBehaviour {
[MenuItem("PlayMode/FPS")]
static void Fps() {
GameObject fps = new GameObject("FPS");
fps.transform.position = Vector3.zero;
GameObject cam = new GameObject("Camera");
cam.transform.parent = fps.transform;
// Camera add component
cam.AddComponent<Camera>();
cam.AddComponent<GUILayer>();
cam.AddComponent<FlareLayer>();
cam.AddComponent<AudioListener>();
}
}
影片教學
FPS來自製FPS來範例好了 =3=
FPS (C#):
using UnityEngine;
using System.Collections;
using UnityEditor;
[RequireComponent(typeof(PlayerMotor))]
public class FPS : MonoBehaviour {
[SerializeField]
private float speed = 5f;
[SerializeField]
private float jumpHeight = 5f;
[SerializeField]
private float lookSensitivity = 3f;
private PlayerMotor motor;
[MenuItem("PlayMode/FPS")]
static void Fps() {
GameObject fps = new GameObject("FPS");
fps.transform.position = Vector3.zero;
fps.AddComponent<FPS>();
GameObject cam = new GameObject("Camera");
cam.transform.parent = fps.transform;
// Camera add component
cam.AddComponent<Camera>();
cam.AddComponent<GUILayer>();
cam.AddComponent<FlareLayer>();
cam.AddComponent<AudioListener>();
}
void Start() {
motor = GetComponent<PlayerMotor>();
}
void Update() {
// Calculate movement veloccity a 3D vector
float _x = Input.GetAxisRaw("Horizontal");
float _z = Input.GetAxisRaw("Vertical");
Vector3 horizontal = transform.right * _x;
Vector3 vertical = transform.forward * _z;
// Final movement vector
Vector3 _velocity = (horizontal + vertical).normalized * speed;
// Apply movement Move
motor.Move(_velocity);
// Calculate movement veloccity a 3D vector
float _y = Input.GetAxisRaw("Jump");
Vector3 up = transform.up * _y;
// Final movement vector
Vector3 jump = (up).normalized * jumpHeight;
// Apply movement Move : Jump
motor.Jump(jump);
//Calculate camera rotation as a 3D vector(turning ariund)
float _mouseY = Input.GetAxisRaw("Mouse X");
Vector3 _MouseRotation = new Vector3(0,_mouseY, 0) * lookSensitivity;
//Apply rotation
motor.Roate(_MouseRotation);
//Calculate camera rotation as a 3D vector(turning ariund)
float _mouseX = Input.GetAxisRaw("Mouse Y");
Vector3 _MouseRotationX = new Vector3(_mouseX, 0, 0) * lookSensitivity;
//Apply camera rotation
motor.CameraRoate(_MouseRotationX);
}
}
PlayerMoto (物件控制腳本)(C#):
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class PlayerMotor : MonoBehaviour {
[SerializeField]
private Camera cam;
private Rigidbody rb;
private Vector3 velocity = Vector3.zero;
private Vector3 jump = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 cameraRotation = Vector3.zero;
// Player Jump
public void Jump(Vector3 _jump) {
jump = _jump;
}
// Player Move
public void Move(Vector3 _velocity) {
velocity = _velocity;
}
// Player Roate
public void Roate(Vector3 _rotation) {
rotation = _rotation;
}
// Player CameraRota
public void CameraRoate(Vector3 _cameraRotation) {
cameraRotation = _cameraRotation;
}
void Start() {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
fexfromMove();
fexformRotation();
}
// Plaer Move setting
void fexfromMove() {
if (velocity != Vector3.zero) {
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
if (jump != Vector3.zero) {
rb.MovePosition(rb.position + jump * Time.fixedDeltaTime);
}
}
// Player Move Roate Setting
void fexformRotation() {
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
GameObject camera = GameObject.Find("FPS/Camera");
cam = camera.GetComponent<Camera>();
if (cam != null) {
cam.transform.Rotate(-cameraRotation);
}
}
}
沒有留言:
張貼留言