Unity版本:2019.2.3f1
目錄
?安裝InputSystem
1:創(chuàng)建InputHander.cs腳本?掛載到Player物體上?獲取鍵盤輸入WADS
2.創(chuàng)建PlayerLocomotion.cs掛載到Player物體上,控制物體移動轉(zhuǎn)向
?
?安裝InputSystem
菜單欄/Window/Package Manager/Input System
?工程面板內(nèi)?右鍵-->創(chuàng)建Input Actions?
選中New Controls改名為PlayerControls?然后屬性?面板按下Edit asset
?Action Maps添加:PlayerMovement
Actions添加:New action?改名為MovementAction?
Properties項(xiàng)? ? 修改ActionType=Pass Through
? ? ? ? ? ? ? ? ? ? ? ? 修改ControlType= Vector2
在MovementAction項(xiàng)點(diǎn)擊+號?選擇Add 2D Vector Composite
?
?生成WASD
綁定Up、Down、Left、Right,如此類推
回到PlayerControls屬性面板?勾選Generate C# Class[*]
?
?工程面板就生成了一份?PlayerControls.cs?腳本,先不管它?
?
?
然后創(chuàng)建2個(gè)腳本掛到Player物體上
文章來源:http://www.zghlxwxcb.cn/news/detail-739735.html
1:創(chuàng)建InputHander.cs腳本?掛載到Player物體上?獲取鍵盤輸入WADS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputHander : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmount;
public float mouseX;
public float mouseY;
PlayerControls inputActions;//聲明 InputSystem的腳本對象
Vector2 movementInput;//存儲 WASD輸入的值
public void OnEnable()
{
//獲取設(shè)備上的輸入
if (inputActions == null)
{
inputActions = new PlayerControls();
//綁定輸入的值
inputActions.PlayerMovement.MovementAction.performed += outputActions => movementInput = outputActions.ReadValue<Vector2>();
}
inputActions.Enable();//啟用
}
public void OnDisable()
{
inputActions.Disable();//禁用
}
public void TickInputt(float delta)
{
MoveInput(delta);
}
public void MoveInput(float delta) {
horizontal = movementInput.x;
vertical = movementInput.y;
moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
}
}
2.創(chuàng)建PlayerLocomotion.cs掛載到Player物體上,控制物體移動轉(zhuǎn)向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLocomotion : MonoBehaviour
{
InputHander inputHander;
Vector3 moveDirection;
public CapsuleCollider capsuleCollider;
public new Rigidbody rigidbody;
Vector2 movementInput;//存儲 WASD輸入的值
Transform cameraObject;
[HideInInspector]
public Transform myTransform;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
if (rigidbody==null)
{
rigidbody = gameObject.AddComponent<Rigidbody>();
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
capsuleCollider = GetComponent<CapsuleCollider>();
if (capsuleCollider==null)
{
capsuleCollider = gameObject.AddComponent<CapsuleCollider>();
capsuleCollider.center = new Vector3(0, 1, 0);
capsuleCollider.radius = 0.5f;
capsuleCollider.height = 2;
capsuleCollider.direction = 1;
}
inputHander = GetComponent<InputHander>();
cameraObject = Camera.main.transform;
myTransform = transform;
}
// Update is called once per frame
void Update()
{
float delta = Time.deltaTime;
inputHander.TickInputt(delta);
moveDirection = cameraObject.forward * inputHander.vertical;
moveDirection += cameraObject.right * inputHander.horizontal;
moveDirection.Normalize();
float speed = movementSpeed;
moveDirection *= speed;
//移動
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, new Vector3(0, 0, 0));
rigidbody.velocity = projectedVelocity;
HandleRotation(delta);
}
//轉(zhuǎn)向
private void HandleRotation(float delta) {
Vector3 targerDir = Vector3.zero;
targerDir = cameraObject.forward * inputHander.vertical;
targerDir += cameraObject.right * inputHander.horizontal;
targerDir.Normalize();
targerDir.y = 0;
if (targerDir == Vector3.zero)
{
targerDir = myTransform.forward;
}
Quaternion tr = Quaternion.LookRotation(targerDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rotationSpeed * delta);
myTransform.rotation = targetRotation;
}
}
完成文章來源地址http://www.zghlxwxcb.cn/news/detail-739735.html
到了這里,關(guān)于Unity解決:3D開發(fā)模式第三人稱視角 WASD控制角色移動旋轉(zhuǎn) 使用InputSystem的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!