FPS第一人稱視角射擊遊戲
遊戲涉及知識點介紹:
- unity對象使用
- 物體旋轉、移動
- 剛體碰撞
- 射線使用
- 協程函數
- 預設體
遊戲下載http://download.csdn.net/detail/d276031034/9759475
遊戲源碼下載http://download.csdn.net/detail/d276031034/9758902
本人是工作中轉到unity開發,在做這款遊戲過程中遇到太多太多的坑。希望能夠一起交流學習
請留言或者聯繫郵箱:[email protected]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
//玩家對象移動速度
public float speed = 9.0f;
//玩家受到的重力
public float gravity = -9.8f;
private CharacterController _characterController;
// Use this for initialization
void Start () {
//獲取對象上CharacterController控件,
/*
* Character Controller本身自帶一個碰撞器,無需剛體即可完成觸發(Trigger)和碰撞(Collision)功能
* 既通過控件CharacterController進行移動物體,自動檢測碰撞
*/
_characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//獲取鍵盤輸入(AD左右按鍵)
float deltaX = Input.GetAxis("Horizontal") * speed;
//獲取鍵盤輸入(WS上下按鍵)
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
//對速度進行限制,向量值最大爲speed
movement = Vector3.ClampMagnitude(movement, speed);
//設置受到重力
movement.y = gravity;
//增量時間,使得在不同的硬件設備運行出來的速度是一致
movement *= Time.deltaTime;
//座標轉換,由自身座標轉換爲世界座標
movement = transform.TransformDirection(movement);
//玩家對象移動
_characterController.Move(movement);
}
}