//========================================================
// 描 述: 很高兴见到你!小王同学,又是精彩的一天,我们一起加油吧!^<>^
// 作 者: 爱吃苹果的小王同学
// 时 间: 2018/10/17 16:31:00
// 版 本: V 1.0
//========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCtr : MonoBehaviour {
private Rigidbody _rig;
private float _h;
private float _v;
public float _moveSpeed;
private Vector3 _movePosition;
private PlayerAttackCtr _attackCtr;
// Use this for initialization
void Start () {
_h = 0;
_v = 0;
if (_moveSpeed == 0)
{
_moveSpeed = 5;
}
_rig = GetComponent<Rigidbody>();
if (_rig==null)
{
_rig = gameObject.AddComponent<Rigidbody>();
}
_rig.useGravity = false;
//冻结刚体轴向操作
_rig.constraints = RigidbodyConstraints.FreezePositionY |
RigidbodyConstraints.FreezeRotationX |
RigidbodyConstraints.FreezeRotationZ;
_movePosition = Vector3.zero;
_attackCtr = GetComponent<PlayerAttackCtr>();
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
if (_attackCtr.PlayerIsDeath)
{
//是否受到物理影响
_rig.isKinematic = true;
return;
}
#region 移动
_h = Input.GetAxisRaw("Horizontal");
_v = Input.GetAxisRaw("Vertical");
_movePosition.Set(_h, 0, _v);
_movePosition = _movePosition.normalized * _moveSpeed;
_rig.MovePosition(transform.position - _movePosition * Time.deltaTime);
#endregion
#region 旋转
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit,100,1<<8))
{
Vector3 v3 = new Vector3(hit.point.x, transform.position.y, hit.point.z);
v3 = v3 - transform.position;
transform.rotation = Quaternion.LookRotation(-v3.normalized);
}
#endregion
}
}
角色简单控制
最新推荐文章于 2024-02-26 09:52:58 发布