'Character is not able to move by using (A , S , D) in unity
Today I watched an video on how to make movement in youtube (https://www.youtube.com/watch?v=bzheMJQVtBI) but the movement occur only when I press W key or change the direction not by using (A, S, D keys) anybody idea how to solve this problem.
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; }
}
}
Solution 1:[1]
//Upward movement - W
if(Input.GetKey(KeyCode.W))
{
print("w");
transform.Translate(Vector3.up * Time.deltaTime * 1);
}
//Move down - S
if(Input.GetKey(KeyCode.S))
{
print("s");
transform.Translate(Vector3.down * Time.deltaTime * 1);
}
//Move to the left - A
if(Input.GetKey(KeyCode.A))
{
print("a");
transform.Translate(Vector3.left * Time.deltaTime * 1);
}
//move right - D
if(Input.GetKey(KeyCode.D))
{
print("d");
transform.Translate(Vector3.right * Time.deltaTime * 1);
}
This is some introduction about unity mobile hope it can help you thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Housheng-MSFT |