I was making a movement script in Unity when an error popped up that I don't know how to fix.
**Input Handler Script**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JG
public class JG : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmount;
public float mouseX;
public float mouseY;
PlayerControls inputActions;
Vector2 movementInput;
Vector2 cameraInput;
public void OnEnable()
{
if (inputActions == null)
{
inputActions = new PlayerControls();
inputActions.PlayerMovement.Movement.performed += inputActions = inputActions.ReadValue();
inputActions.Camera.performed += i => cameraInput = i.ReadValue();
}
inputActions.Enable();
}
private void OnDisable()
{
inputActions.Disable();
}
public void TickInput(float delta)
{
MoveInput(delta);
}
private void MoveInput(float delta)
{
horizontal = movementInput.x;
vertical = movementInput.y;
moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
mouseX = cameraInput.x;
mouseY = cameraInput.y;
}
}
}
**Player Locomotion Script**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JG
{
public class PlayerLocomotion : MonoBehaviour
{
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializedField]
float movementSpeed = 5;
[SerializedField]
float rotationSpeed = 10;
void Start()
{
rigidbody = GetComponent();
inputHandler = GetComponent();
cameraObject = Camera.main.Transform;
myTransform = transform;
}
public void Update()
{
float delta = Time.deltaTime;
inputHandler.TickInput(delta);
moveDirection = cameraObject.forward = inputHandler.vertical;
moveDirection += cameraObject.right = inputHandler.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 (float delta)
{
Vector3 targetDirection = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDirection = cameraObject.forward * inputHandler.vertical;
targetDirection = cameraObject.right * inputHandler.horizontal;
targetDirection.Normalize();
targetDirection.y = 0;
if(targetDirection == Vector3.zero)
targetDirection = myTransform.forward;
float rs = rotationSpeed
Quaternion tr = Quaternion.LookRotation(targetDirection);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, true, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
↧