【发布时间】:2022-11-14 00:07:12
【问题描述】:
我是 根据本教程制作第三人称控制器 https://youtu.be/suU4aBdBjKA 一个问题是。我的角色会向左走,但面向右侧。 如果我往前走,我的角色会面朝后,而不是往前走。 我尝试旋转角色,但没有成功,因为角色会弹回原位。 虽然这不是问题。 唯一的问题是,角色没有面对它要去的地方。
我尝试旋转角色,但没有做任何事情。这仅在角色移动时。它看起来与它正在走向相反的方向。
我尝试在手柄旋转“=”下加一个加号 但这并没有做任何事情,因为我正在寻找视频中的 cmets,但什么也没找到。
这是代码。有什么我可以解决的吗?
这是 PLAYER LOCAL MOTION `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLocomotion : MonoBehaviour
{
InputManager inputManager;
Vector3 moveDirection;
Transform cameraObject;
Rigidbody playerRigidBody;
public float movementSpeed = 7;
public float rotationSpeed = 15;
private void Awake()
{
inputManager = GetComponent<InputManager>();
playerRigidBody = GetComponent<Rigidbody>();
cameraObject = Camera.main.transform;
}
public void HandleAllMovement()
{
HandleMovement();
HandleRotation();
}
private void HandleMovement()
{
moveDirection = cameraObject.forward * inputManager.verticalInput;
moveDirection = moveDirection + cameraObject.right * inputManager.horizontalInput;
moveDirection.Normalize();
moveDirection.y = 0;
moveDirection = moveDirection * movementSpeed;
Vector3 movementVelocity = moveDirection;
playerRigidBody.velocity = movementVelocity;
}
private void HandleRotation()
{
Vector3 targetDirection = Vector3.zero;
targetDirection = cameraObject.forward * inputManager.verticalInput;
targetDirection = targetDirection + cameraObject.right * inputManager.horizontalInput;
targetDirection.Normalize();
targetDirection.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
Quaternion playerRotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
transform.rotation = playerRotation;
}
}
`
这是球员经理 `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
InputManager inputManager;
PlayerLocomotion playerLocomotion;
private void Awake()
{
inputManager = GetComponent<InputManager>();
playerLocomotion = GetComponent<PlayerLocomotion>();
}
private void Update()
{
inputManager.HandleAllInputs();
}
private void FixedUpdate()
{
playerLocomotion.HandleAllMovement();
}
}
Here's the INPUT MANAGER
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager : MonoBehaviour
{
PlayerControls playerControls;
public Vector2 movementInput;
public float verticalInput;
public float horizontalInput;
private void OnEnable()
{
if (playerControls == null)
{
playerControls = new PlayerControls();
playerControls.PlayerMovement.Movement.performed += i => movementInput = i.ReadValue<Vector2>();
}
playerControls.Enable();
}
private void OnDisable()
{
playerControls.Disable();
}
public void HandleAllInputs()
{
HandleMovementInput();
//HandleJumpingInput
//HandleActionInput
}
private void HandleMovementInput()
{
verticalInput = movementInput.y;
horizontalInput = movementInput.x;
}
}
`
它们都连接在一起。我只是无法理解或理解可能导致玩家以这种方式移动的问题所在。
任何帮助表示赞赏。这就是团结。 谢谢你。
【问题讨论】:
标签: c# unity3d controller blender unityscript