Skip to content

Instantly share code, notes, and snippets.

@SlashScreen
Last active August 3, 2020 00:14
Show Gist options
  • Save SlashScreen/460ed0d5dbcbaa189e99af18c08fa694 to your computer and use it in GitHub Desktop.
Save SlashScreen/460ed0d5dbcbaa189e99af18c08fa694 to your computer and use it in GitHub Desktop.
for the unity input problem
https://imgur.com/rBqGZhr - Player setup
https://imgur.com/v27VT5w - Control setup
https://imgur.com/FgN3GDG - PlayerControlInput script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class mouselook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerbody;
public PlayerControlInput control;
float xRot = 0f;
private void Awake()
{
control = new PlayerControlInput();
control.Player.Look.performed += ctx => MouseLook((ctx.ReadValue<Vector2>()));
}
private void OnEnable()
{
control.Enable();
}
private void OnDisable()
{
control.Disable();
}
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void MouseLook(Vector2 MouseData){
float mouseX = MouseData.x * mouseSensitivity * Time.deltaTime;
float mouseY = MouseData.y * mouseSensitivity * Time.deltaTime;
playerbody.Rotate(Vector3.up * mouseX);
xRot -= mouseY;
xRot = Mathf.Clamp(xRot, -90f, 90f); //TODO put this clamp in input
transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class playermovement : MonoBehaviour
{
public CharacterController controller;
public PlayerControlInput control;
public float speed = 10f;
public float gravity = -10f;
public float jumpHeight = 5f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float playerheight = 2f;
public float crouchedheight = 1f;
Vector3 velocity;
bool isGrounded;
void Awake()
{
control = new PlayerControlInput();
control.Player.Jump.performed += _ => Jump();
control.Player.Move.performed += ctx => DoMovement(ctx.ReadValue<Vector2>());
}
private void OnEnable()
{
control.Enable();
}
private void OnDisable()
{
control.Disable();
}
void Jump(){
//Process jumping
if (isGrounded){
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
//TODO: Crouch
void DoMovement(Vector2 direction){
//Process horizontal movement
float x = direction.x;
float z = direction.y;
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
}
void Update()
{
//gravity processing
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0){
velocity.y = 0f;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment