Question by
ERStudios · Mar 22, 2021 at 11:25 AM ·
3rd person controller
Player don't stop moving Pls help, thanks in advance.
using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace EB { public class playerlocomotion : MonoBehaviour { Transform cameraobject;
inputmano inputHandeler;
Vector3 movedirection;
[HideInInspector]
public Transform mytransform;
public new Rigidbody rigidbody;
public GameObject normalcamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandeler = GetComponent<inputmano>();
cameraobject = Camera.main.transform;
mytransform = transform;
}
public void Update()
{
float delta = Time.deltaTime;
inputHandeler.TickInput(delta);
movedirection = cameraobject.forward * inputHandeler.vertical;
movedirection += cameraobject.right * inputHandeler.horizontal;
movedirection.Normalize();
float speed = movementSpeed;
movedirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(movedirection, normalVector);
rigidbody.velocity = projectedVelocity;
}
#region Movement
Vector3 normalVector;
Vector3 targetposition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandeler.moveamout;
targetDir = cameraobject.forward * inputHandeler.vertical;
targetDir += cameraobject.right * inputHandeler.horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = mytransform.forward;
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(mytransform.rotation, tr, rs * delta);
mytransform.rotation = targetRotation;
}
#endregion
}
}
Other script
using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace EB { public class inputmano : MonoBehaviour { public float horizontal; public float vertical; public float moveamout; public float mousex; public float mousey;
Playercontroller inputactions;
Vector2 movementinput;
Vector2 camerainput;
public void Update()
{
if (inputactions == null)
{
inputactions = new Playercontroller();
inputactions.PlayerMovement.movement.performed += inputactions => movementinput = inputactions.ReadValue<Vector2>();
inputactions.PlayerMovement.camera.performed += i => camerainput = i.ReadValue<Vector2>();
}
inputactions.Enable();
}
private void OnDisabled()
{
inputactions.Disable();
}
public void TickInput(float delta)
{
MoveInput(delta);
}
private void MoveInput(float delta)
{
horizontal = movementinput.x;
vertical = movementinput.y;
moveamout = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
mousex = camerainput.x;
mousey = camerainput.y;
}
}
}
Comment