Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges