角色无法通过统一使用(A,S,D)移动
Character is not able to move by using (A , S , D) in unity
今天我在 youtube 上看了一个关于如何移动的视频 (https://www.youtube.com/watch?v=bzheMJQVtBI),但是移动只有在我按下 W 键或改变方向时才会发生,而不是通过使用 (A、S、D 键)任何人想法如何解决这个问题。
AgentMovemnt.cs
using UnityEngine;
namespace SVS
{
public class AgentMovement : MonoBehaviour
{
CharacterController controller;
Animator animator;
public float rotationSpeed, movementSpeed, gravity = 20;
Vector3 movementVector = Vector3.zero;
private float desiredRotationAngle = 0;
private void Start()
{
controller = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
}
public void HandleMovement(Vector2 input)
{
if (controller.isGrounded)
{
if(input.y > 0)
{
movementVector = transform.forward * movementSpeed;
}
else
{
movementVector = Vector3.zero;
animator.SetFloat("move", 0);
}
}
}
public void HandleMovementDirection(Vector3 direction)
{
desiredRotationAngle = Vector3.Angle(transform.forward, direction);
var crossProduct = Vector3.Cross(transform.forward, direction).y;
if (crossProduct < 0)
{
desiredRotationAngle *= -1;
}
}
private void RotateAgent()
{
if(desiredRotationAngle > 10 || desiredRotationAngle < -10)
{
transform.Rotate(Vector3.up * desiredRotationAngle * rotationSpeed * Time.deltaTime);
}
}
private float SetCorrectAnimation()
{
float currentAnimationSpeed = animator.GetFloat("move");
if(desiredRotationAngle > 10 || desiredRotationAngle < -10)
{
if(currentAnimationSpeed < 0.2f)
{
currentAnimationSpeed += Time.deltaTime * 2;
currentAnimationSpeed = Mathf.Clamp(currentAnimationSpeed, 0, 0.2f);
}
animator.SetFloat("move", currentAnimationSpeed);
}
else
{
if (currentAnimationSpeed < 1)
{
currentAnimationSpeed += Time.deltaTime * 2;
}
else
{
currentAnimationSpeed = 1;
}
animator.SetFloat("move", currentAnimationSpeed);
}
return currentAnimationSpeed;
}
private void Update()
{
if (controller.isGrounded)
{
if (movementVector.magnitude > 0)
{
var animationSpeedMultiplier = SetCorrectAnimation();
RotateAgent();
movementVector *= animationSpeedMultiplier;
}
}
movementVector.y -= gravity;
controller.Move(movementVector * Time.deltaTime);
}
}
}
AgentController.cs
using UnityEngine;
namespace SVS
{
public class AgentController : MonoBehaviour
{
IInput input;
AgentMovement movement;
private void OnEnable()
{
input = GetComponent<IInput>();
movement = GetComponent<AgentMovement>();
input.OnMovementDirectionInput += movement.HandleMovementDirection;
input.OnMovementInput += movement.HandleMovement;
}
private void OnDisable()
{
input.OnMovementDirectionInput -= movement.HandleMovementDirection;
input.OnMovementInput -= movement.HandleMovement;
}
}
}
PlayerInput.cs
using System;
using UnityEngine;
namespace SVS
{
public class PlayerInput : MonoBehaviour, IInput
{
public Action<Vector2> OnMovementInput { get; set; }
public Action<Vector3> OnMovementDirectionInput { get; set; }
private void Start()
{
//Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
GetMovementInput();
GetMovementDirection();
}
private void GetMovementDirection()
{
var cameraForewardDIrection = Camera.main.transform.forward;
Debug.DrawRay(Camera.main.transform.position, cameraForewardDIrection * 10, Color.red);
var directionToMoveIn = Vector3.Scale(cameraForewardDIrection, (Vector3.right + Vector3.forward));
Debug.DrawRay(Camera.main.transform.position, directionToMoveIn * 10, Color.blue);
OnMovementDirectionInput?.Invoke(directionToMoveIn.normalized);
}
private void GetMovementInput()
{
Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
OnMovementInput?.Invoke(input);
}
}
}
IInput.cs
using System;
using UnityEngine;
namespace SVS
{
public interface IInput
{
Action<Vector3> OnMovementDirectionInput { get; set; }
Action<Vector2> OnMovementInput { get; set; }
}
}
//向上运动-W
if(Input.GetKey(KeyCode.W))
{
print("w");
transform.Translate(Vector3.up * Time.deltaTime * 1);
}
//下移-S
if(Input.GetKey(KeyCode.S))
{
print("s");
transform.Translate(Vector3.down * Time.deltaTime * 1);
}
//向左移动-A
if(Input.GetKey(KeyCode.A))
{
print("a");
transform.Translate(Vector3.left * Time.deltaTime * 1);
}
//向右移动 - D
if(Input.GetKey(KeyCode.D))
{
print("d");
transform.Translate(Vector3.right * Time.deltaTime * 1);
}
关于unity mobile的一些介绍希望对大家有所帮助谢谢
今天我在 youtube 上看了一个关于如何移动的视频 (https://www.youtube.com/watch?v=bzheMJQVtBI),但是移动只有在我按下 W 键或改变方向时才会发生,而不是通过使用 (A、S、D 键)任何人想法如何解决这个问题。
AgentMovemnt.cs
using UnityEngine;
namespace SVS
{
public class AgentMovement : MonoBehaviour
{
CharacterController controller;
Animator animator;
public float rotationSpeed, movementSpeed, gravity = 20;
Vector3 movementVector = Vector3.zero;
private float desiredRotationAngle = 0;
private void Start()
{
controller = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
}
public void HandleMovement(Vector2 input)
{
if (controller.isGrounded)
{
if(input.y > 0)
{
movementVector = transform.forward * movementSpeed;
}
else
{
movementVector = Vector3.zero;
animator.SetFloat("move", 0);
}
}
}
public void HandleMovementDirection(Vector3 direction)
{
desiredRotationAngle = Vector3.Angle(transform.forward, direction);
var crossProduct = Vector3.Cross(transform.forward, direction).y;
if (crossProduct < 0)
{
desiredRotationAngle *= -1;
}
}
private void RotateAgent()
{
if(desiredRotationAngle > 10 || desiredRotationAngle < -10)
{
transform.Rotate(Vector3.up * desiredRotationAngle * rotationSpeed * Time.deltaTime);
}
}
private float SetCorrectAnimation()
{
float currentAnimationSpeed = animator.GetFloat("move");
if(desiredRotationAngle > 10 || desiredRotationAngle < -10)
{
if(currentAnimationSpeed < 0.2f)
{
currentAnimationSpeed += Time.deltaTime * 2;
currentAnimationSpeed = Mathf.Clamp(currentAnimationSpeed, 0, 0.2f);
}
animator.SetFloat("move", currentAnimationSpeed);
}
else
{
if (currentAnimationSpeed < 1)
{
currentAnimationSpeed += Time.deltaTime * 2;
}
else
{
currentAnimationSpeed = 1;
}
animator.SetFloat("move", currentAnimationSpeed);
}
return currentAnimationSpeed;
}
private void Update()
{
if (controller.isGrounded)
{
if (movementVector.magnitude > 0)
{
var animationSpeedMultiplier = SetCorrectAnimation();
RotateAgent();
movementVector *= animationSpeedMultiplier;
}
}
movementVector.y -= gravity;
controller.Move(movementVector * Time.deltaTime);
}
}
}
AgentController.cs
using UnityEngine;
namespace SVS
{
public class AgentController : MonoBehaviour
{
IInput input;
AgentMovement movement;
private void OnEnable()
{
input = GetComponent<IInput>();
movement = GetComponent<AgentMovement>();
input.OnMovementDirectionInput += movement.HandleMovementDirection;
input.OnMovementInput += movement.HandleMovement;
}
private void OnDisable()
{
input.OnMovementDirectionInput -= movement.HandleMovementDirection;
input.OnMovementInput -= movement.HandleMovement;
}
}
}
PlayerInput.cs
using System;
using UnityEngine;
namespace SVS
{
public class PlayerInput : MonoBehaviour, IInput
{
public Action<Vector2> OnMovementInput { get; set; }
public Action<Vector3> OnMovementDirectionInput { get; set; }
private void Start()
{
//Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
GetMovementInput();
GetMovementDirection();
}
private void GetMovementDirection()
{
var cameraForewardDIrection = Camera.main.transform.forward;
Debug.DrawRay(Camera.main.transform.position, cameraForewardDIrection * 10, Color.red);
var directionToMoveIn = Vector3.Scale(cameraForewardDIrection, (Vector3.right + Vector3.forward));
Debug.DrawRay(Camera.main.transform.position, directionToMoveIn * 10, Color.blue);
OnMovementDirectionInput?.Invoke(directionToMoveIn.normalized);
}
private void GetMovementInput()
{
Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
OnMovementInput?.Invoke(input);
}
}
}
IInput.cs
using System;
using UnityEngine;
namespace SVS
{
public interface IInput
{
Action<Vector3> OnMovementDirectionInput { get; set; }
Action<Vector2> OnMovementInput { get; set; }
}
}
//向上运动-W
if(Input.GetKey(KeyCode.W))
{
print("w");
transform.Translate(Vector3.up * Time.deltaTime * 1);
}
//下移-S
if(Input.GetKey(KeyCode.S))
{
print("s");
transform.Translate(Vector3.down * Time.deltaTime * 1);
}
//向左移动-A
if(Input.GetKey(KeyCode.A))
{
print("a");
transform.Translate(Vector3.left * Time.deltaTime * 1);
}
//向右移动 - D
if(Input.GetKey(KeyCode.D))
{
print("d");
transform.Translate(Vector3.right * Time.deltaTime * 1);
}
关于unity mobile的一些介绍希望对大家有所帮助谢谢