Character Contoller Script
Just starting out in Unity. Looking for some help with my character controller code. Right now the character just keeps on moving when you press a key and never stops. I am using the new inputsystem.
Thanks!
using UnityEngine; using UnityEngine.InputSystem;
public class PCContoller_v2 : MonoBehaviour { public Rigidbody character; public float moveSpeed = 100; //private float sprintMult = 2; private Vector3 moveVelocity = Vector3.zero; private Vector3 movement; //Input Actions private PlayerActionControls inputAction; //Move Input from user private Vector2 keyboardInput; private void Awake() { //ctx = context inputAction = new PlayerActionControls();
inputAction.Player.Move.performed +=
ctx => moveVelocity =
ctx.ReadValue<Vector2>();
}
private void OnEnable() => inputAction.Enable();
private void OnDisable() => inputAction.Disable();
private void MovePlayer(Vector3 moveVelocity)
{
movement.Set(moveVelocity.x, 0f, moveVelocity.y);
movement = movement * moveSpeed * Time.deltaTime;
character.AddForce(moveVelocity + movement);
}
private void GetUserInput()
{
inputAction.Player.Move.performed +=
ctx => moveVelocity =
ctx.ReadValue<Vector2>();
}
void FixedUpdate()
{
float x = moveVelocity.x;
float y = moveVelocity.y;
var userControlsInput = new Vector3(x, y, 0);
//character.AddForce(userControlsInput * Time.deltaTime);
//MovePlayer(moveVelocity);
}
}
Your answer
Follow this Question
Related Questions
Errors With Game Over Script 1 Answer
Dreamlo Leaderbord issue 0 Answers
I need help with my scripts! 1 Answer