ARPG遊戲開發學習2——進入遊戲、主角生成與控制
阿新 • • 發佈:2020-08-24
player新增Character Controller元件實現碰撞效果
角色控制程式碼如下:
InputC
public class InputC : MonoBehaviour { public static InputC instance; public Vector2 m_Movement; public Vector3 m_Camera; //觀察者模式 public event UnityAction<Vector2> InputEventUpdate; private void Awake() { instance= this; } void Start() { } void Update() { m_Movement.Set(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); m_Camera.Set(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse ScrollWheel")); if(InputEventUpdate!=null) { InputEventUpdate(m_Movement); } } }
PlayerC
public class PlayerC : MonoBehaviour { private CharacterController controller; public float walkSpeed = 4.5f; public static PlayerC instance; InputC m_Input; //監聽事件 private void OnEnable() { InputC.instance.InputEventUpdate+= FuncUpdate; } private void OnDisable() { InputC.instance.InputEventUpdate -= FuncUpdate; } private void Awake() { instance = this; controller = GetComponent<CharacterController>(); m_Input = FindObjectOfType<InputC>(); } private void Move() { //float H = Input.GetAxis("Horizontal"); //float V = Input.GetAxis("Vertical"); Vector3 dir = transform.TransformDirection(new Vector3(m_Input.m_Movement.x, -1, m_Input.m_Movement.y)); controller.Move(dir * walkSpeed * Time.deltaTime); } void Start() { } void Update() { // Move(); } void FuncUpdate(Vector2 m_Movement) { Vector3 dir = transform.TransformDirection(new Vector3(m_Movement.x, -1, m_Movement.y)); controller.Move(dir * walkSpeed * Time.deltaTime); } }
PlayerAni
public class PlayerAni : MonoBehaviour { public Animator ani; InputC m_Input; void Start() { m_Input = FindObjectOfType<InputC>(); } void Update() { ani.SetFloat("horizontal", m_Input.m_Movement.x); ani.SetFloat("vertical", m_Input.m_Movement.y); } }
ARPG攝像機
ARPGcameraC
public class ARPGcameraC : MonoBehaviour { public Transform target; public float targetHeight = 1.8f; //到目標距離的高度 public float targetSide = -0.1f; //到目標距離的偏移值(水平方向) public float distance = 4; //距離(拉近拉遠) public float maxDistance = 8; //最大距離 public float minDistance = 2.2f; //最小距離 public float xSpeed = 250; //水平旋轉速度 public float ySpeed = 125; //垂直旋轉速度 public float yMinLimit = -10; //旋轉上角度 public float yMaxLimit = 72; //旋轉下角度 public float zoomRate = 80; //縮放倍數 public float x =20; public float y =0; InputC m_Input; private void Awake() { m_Input = FindObjectOfType<InputC>(); //隱藏滑鼠 Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } void Start() { } void Update() { } private void LateUpdate() { x += m_Input.m_Camera.x * xSpeed * Time.deltaTime; y -= m_Input.m_Camera.y * ySpeed * Time.deltaTime; distance -= (m_Input.m_Camera.z * Time.deltaTime) * zoomRate * Mathf.Abs(distance); distance = Mathf.Clamp(distance, minDistance, maxDistance); y = ClampAngle(y,yMinLimit,yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x,0); transform.rotation = rotation; if (m_Input.m_Camera.x != 0 || m_Input.m_Camera.y != 0) { target.transform.rotation = Quaternion.Euler(0, x, 0); } transform.position = target.position - ((rotation * new Vector3(targetSide, 0, 1)*distance - new Vector3(0,targetHeight,0))); } float ClampAngle(float angle,float min, float max) { if(angle<-360) { angle += 360; } if(angle>360) { angle -= 360; } return Mathf.Clamp(angle, min, max); } }
效果預覽: