赞
踩
前文:https://blog.csdn.net/Jaihk662/article/details/87906156(Input控制面板)
为什么使用角色控制器而不使用刚体?
→ 刚体过于灵活(敏感),例如稍有碰撞就会被弹开,物体的质量不好把握或计算等等
角色控制器面板与属性:
OnControllerColliderHit(ControllerColliderHit hit):角色控制器自带有碰撞器,hit为角色碰撞器碰撞到的物体的信息,除此之外,如果拥有角色控制器组件,那么共存的其他的碰撞器组件就会处于“无效”状态
角色碰撞器的所有属性也都可以通过脚本获取,另一个控制角色移动脚本示例如下:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMove : MonoBehaviour
- {
- private Rigidbody myRigidbody;
- private Transform myTran;
- private CharacterController cont;
- public float moveSpeed = 12.0f;
- public float turnSpeed = 180.0f;
- private float myMoveAxis = 0.0f;
- private float myTurnAxis = 0.0f;
- void Start()
- {
- myRigidbody = gameObject.GetComponent<Rigidbody>();
- myTran = gameObject.GetComponent<Transform>();
- cont = gameObject.GetComponent<CharacterController>();
- cont.skinWidth = 0.02f; //初始化角色的皮肤厚度
- }
- void FixedUpdate()
- {
- myMoveAxis = Input.GetAxis("Vertical");
- myTurnAxis = Input.GetAxis("Horizontal");
- Move(myMoveAxis);
- Turn(myTurnAxis);
- }
- void Move(float myMoveAxis)
- {
- //Vector3 moveChange = myTran.forward * myMoveAxis * Time.deltaTime * moveSpeed;
- //myRigidbody.MovePosition(myTran.position + moveChange);
- if (myMoveAxis > 0) //角色前进
- cont.Move(myTran.forward * Time.deltaTime * moveSpeed * myMoveAxis);
- else if (myMoveAxis < 0) //角色后退
- {
- //Quaternion turnChange = Quaternion.Euler(0f, 90f, 0f);
- //myRigidbody.MoveRotation(myRigidbody.rotation * turnChange);
- cont.Move(myTran.forward * Time.deltaTime * moveSpeed * myMoveAxis/2);
- }
- }
- void Turn(float myTurnAxis)
- {
- float Change = myTurnAxis * turnSpeed * Time.deltaTime;
- Quaternion turnChange = Quaternion.Euler(0f, Change, 0f);
- myRigidbody.MoveRotation(myRigidbody.rotation * turnChange);
- }
- void OnControllerColliderHit(ControllerColliderHit hit)
- {
- Debug.Log(hit.gameObject.name);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。