Question by
CIean · Apr 09, 2021 at 03:53 PM ·
lightingfpslightfpscontrollerfps-controller
Idk how to make a flashlight with the new input system :(
I am very confused with the new unity input system, I read it, but I can't seem to get a light to turn on and off, if anyone could help me I would appreciate it.
These are the character scripts:
Models.cs
using System;
using System.Collections.Generic;
using UnityEngine;
public static class Models
{
#region - Player -
public enum PlayerStance
{
Stand,
Crouch,
Prone
}
[Serializable]
public class PlayerSettingsModel
{
[Header("View Settings")]
public float ViewXSensitivity;
public float ViewYSensitivity;
public bool ViewXInverted;
public bool ViewYInverted;
[Header("Movement Settings")]
public bool SprintingHold;
public float MovementSmoothing;
[Header("Movement - Running")]
public float RunningForwardSpeed;
public float RunningStrafeSpeed;
[Header("Movement - Walking")]
public float WalkingForwardSpeed;
public float WalkingBackwardSpeed;
public float WalkingStrafeSpeed;
[Header("Jumping")]
public float JumpingHeight;
public float JumpingFalloff;
public float FallingSmoothing;
[Header("Speed Effectors")]
public float SpeedEffector = 1;
public float CrouchSpeedEffector;
public float ProneSpeedEffector;
public float FallingSpeedEffector;
}
[Serializable]
public class CharacterStance
{
public float CameraHeight;
public CapsuleCollider StanceCollider;
}
#endregion
}
ChrController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Models;
using UnityEngine.InputSystem;
public class ChrController : MonoBehaviour
{
private CharacterController characterController;
private PlayerControlls defaultInput;
private Vector2 input_Movement;
private Vector2 input_View;
private Vector3 newCameraRotation;
private Vector3 newCharacterRotation;
[Header("References")]
public Transform cameraHolder;
public Transform feetTransform;
[Header("Settings")]
public PlayerSettingsModel playerSettings;
public float viewClampYMin = -70f;
public float viewClampYMax = 80f;
public LayerMask playerMask;
[Header("Gravity")]
public float gravityAmount;
public float gravityMin;
private float playerGravity;
public Vector3 jumpingForce;
private Vector3 jumpingForceVelocity;
[Header("Stance")]
public PlayerStance playerStance;
public float playerStanceSmoothing;
public CharacterStance playerStandStance;
public CharacterStance playerCrouchStance;
public CharacterStance playerProneStance;
private float stanceCheckErrorMargin = 0.05f;
private float cameraHeight;
private float cameraHeightVelocity;
private Vector3 stanceCapsuleCenterVelocity;
private float stanceCapsuleHeightVelocity;
private bool isSprinting;
private Vector3 newMovementSpeed;
private Vector3 newMovementSpeedVelocity;
private void Awake()
{
defaultInput = new PlayerControlls();
defaultInput.Character.Movement.performed += e => input_Movement = e.ReadValue<Vector2>();
defaultInput.Character.View.performed += e => input_View = e.ReadValue<Vector2>();
defaultInput.Character.Jump.performed += e => Jump();
defaultInput.Character.Crouch.performed += e => Crouch();
defaultInput.Character.Prone.performed += e => Prone();
defaultInput.Character.Sprint.performed += e => ToggleSprint();
defaultInput.Character.SprintRelease.performed += e => StopSprint();
defaultInput.Enable();
newCameraRotation = cameraHolder.localRotation.eulerAngles;
newCharacterRotation = transform.localRotation.eulerAngles;
characterController = GetComponent<CharacterController>();
cameraHeight = cameraHolder.localPosition.y;
}
private void Update()
{
CalculateView();
CalculateMovement();
CalculateJump();
CalculateStance();
}
private void CalculateView()
{
newCharacterRotation.y += playerSettings.ViewXSensitivity * (playerSettings.ViewXInverted ? -input_View.x : input_View.x) * Time.deltaTime;
transform.localRotation = Quaternion.Euler(newCharacterRotation);
newCameraRotation.x += playerSettings.ViewYSensitivity * (playerSettings.ViewYInverted ? input_View.y : -input_View.y) * Time.deltaTime;
newCameraRotation.x = Mathf.Clamp(newCameraRotation.x, viewClampYMin, viewClampYMax);
cameraHolder.localRotation = Quaternion.Euler(newCameraRotation);
}
private void CalculateMovement()
{
if (input_Movement.y <= 0.2f)
{
isSprinting = false;
}
var verticalSpeed = playerSettings.WalkingForwardSpeed;
var horizontalSpeed = playerSettings.WalkingStrafeSpeed;
if (isSprinting)
{
verticalSpeed = playerSettings.RunningForwardSpeed;
horizontalSpeed = playerSettings.RunningStrafeSpeed;
}
if (!characterController.isGrounded)
{
playerSettings.SpeedEffector = playerSettings.FallingSpeedEffector;
}
else if (playerStance == PlayerStance.Crouch)
{
playerSettings.SpeedEffector = playerSettings.CrouchSpeedEffector;
}
else if (playerStance == PlayerStance.Prone)
{
playerSettings.SpeedEffector = playerSettings.ProneSpeedEffector;
}
else
{
playerSettings.SpeedEffector = 1;
}
verticalSpeed *= playerSettings.SpeedEffector;
horizontalSpeed *= playerSettings.SpeedEffector;
newMovementSpeed = Vector3.SmoothDamp(newMovementSpeed, new Vector3(horizontalSpeed * input_Movement.x * Time.deltaTime, 0, verticalSpeed * input_Movement.y * Time.deltaTime), ref newMovementSpeedVelocity, characterController.isGrounded ? playerSettings.MovementSmoothing : playerSettings.FallingSmoothing);
var movementSpeed = transform.TransformDirection(newMovementSpeed);
if (playerGravity > gravityMin)
{
playerGravity -= gravityAmount * Time.deltaTime;
}
if (playerGravity < -0.1f && characterController.isGrounded)
{
playerGravity = -0.1f;
}
movementSpeed.y += playerGravity;
movementSpeed += jumpingForce * Time.deltaTime;
characterController.Move(movementSpeed);
}
private void CalculateJump()
{
jumpingForce = Vector3.SmoothDamp(jumpingForce, Vector3.zero, ref jumpingForceVelocity, playerSettings.JumpingFalloff);
}
private void CalculateStance()
{
var currentStance = playerStandStance;
if (playerStance == PlayerStance.Crouch)
{
currentStance = playerCrouchStance;
}
else if (playerStance == PlayerStance.Prone)
{
currentStance = playerProneStance;
}
cameraHeight = Mathf.SmoothDamp(cameraHolder.localPosition.y, currentStance.CameraHeight, ref cameraHeightVelocity, playerStanceSmoothing);
cameraHolder.localPosition = new Vector3(cameraHolder.localPosition.x, cameraHeight, cameraHolder.localPosition.z);
characterController.height = Mathf.SmoothDamp(characterController.height, currentStance.StanceCollider.height, ref stanceCapsuleHeightVelocity, playerStanceSmoothing);
characterController.center = Vector3.SmoothDamp(characterController.center, currentStance.StanceCollider.center, ref stanceCapsuleCenterVelocity, playerStanceSmoothing);
}
private void Jump()
{
if (!characterController.isGrounded || playerStance == PlayerStance.Prone)
{
return;
}
if (playerStance == PlayerStance.Crouch)
{
if (StanceCheck(playerStandStance.StanceCollider.height))
{
return;
}
playerStance = PlayerStance.Stand;
return;
}
//Jump
jumpingForce = Vector3.up * playerSettings.JumpingHeight;
playerGravity = 0;
}
private void Crouch()
{
if (playerStance == PlayerStance.Crouch)
{
if (StanceCheck(playerStandStance.StanceCollider.height))
{
return;
}
playerStance = PlayerStance.Stand;
return;
}
if (StanceCheck(playerCrouchStance.StanceCollider.height))
{
return;
}
playerStance = PlayerStance.Crouch;
}
private void Prone()
{
playerStance = PlayerStance.Prone;
}
private bool StanceCheck(float stanceCheckheight)
{
var start = new Vector3(feetTransform.position.x, feetTransform.position.y + characterController.radius + stanceCheckErrorMargin, feetTransform.position.z);
var end = new Vector3(feetTransform.position.x, feetTransform.position.y - characterController.radius - stanceCheckErrorMargin + stanceCheckheight, feetTransform.position.z);
return Physics.CheckCapsule(start, end, characterController.radius, playerMask);
}
private void ToggleSprint()
{
if (input_Movement.y <= 0.2f)
{
isSprinting = false;
return;
}
isSprinting = !isSprinting;
}
private void StopSprint()
{
if (playerSettings.SprintingHold)
{
isSprinting = false;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613032012im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D - Material for light 2 Answers
Spotlight becomes square. -1 Answers
Everything is too bright 1 Answer
Multiplayer Light Intensity Not Syncing 1 Answer
Did Unity used to have only 3D Lights?,Did Unity used to have only 3D lights? 0 Answers