fps script not working
I'm trying to Bild a simple caricature controller with sprinting, walking, crouching, and jumping,
but the sprinting and crouching don't work, Please tell me wot I'm doing Ronge
I did change the inputs under project settings,, input manager,, so the fact that all my keywords are different is not the problem, also all of the buttons work I tested (that's wot the print("words") are for) I know that there is some sort of debug but I'm a bit new at unity C# and also I think changing the inputs messed that up.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//Public Float: Speed
public float WalkSpeed = 10f;
public float RunSpeed = 16f;
public float JumpSpeed = 8f;
public float CrouchSpeed = 8f;
//Public Float: Other
public float JumpHeight = 3f;
public float Gravity = -9.81f;
public float GroundDistance = 0.4f;
public float VScaleUp = 2f;
public float VScaleDown = 1f;
//Objects
public Transform GroundCheck;
public LayerMask GroundMask;
public CharacterController Controller;
Vector3 Velocity;
bool IsGrounded;
// Update Is Called Once Per Frame
void Update()
{
//Speed
float Speed = WalkSpeed;
//VScale
float VScale = VScaleUp;
Controller.height = VScale;
//Input
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Movement Based on Wear The Players Facing
Vector3 move = transform.right * x + transform.forward * z;
Controller.Move(move * Speed * Time.deltaTime);
Controller.Move(Velocity * Time.deltaTime);
//GroundCheck
IsGrounded = Physics.CheckSphere(GroundCheck.position,
GroundDistance, GroundMask);
Velocity.y += Gravity * Time.deltaTime;
//IsGrounded
if (IsGrounded && Velocity.y < 0)
{
Velocity.y =-2f;
}
//Sprint Function
if (Input.GetButtonDown("Sprint") && IsGrounded)
{
print("Sprint!!!");
Speed = RunSpeed;
}
//Jump Function
if (Input.GetButtonDown("Jump") && IsGrounded)
{
Velocity.y = Mathf.Sqrt(JumpHeight * -2f * Gravity);
print("jump!!!");
}
//Crouch Function
if (Input.GetButtonDown("Crouch") && IsGrounded)
{
print("Crouch!!!");
VScale = VScaleDown;
}
}
}
Answer by jimmiewalker653 · Nov 26, 2020 at 03:17 PM
In the Update, you're calling speed = WalkSpeed; You only change it when "Sprint" is pressed once. So, you should change it from GetButtonDown to GetButton. If you want to tap the key, then the way to do this is to create a bool called "isSprinting" (or whatever you want to call it).
bool isSprinting;
void Update()
{
if (Input.GetButtonDown("Sprint"))
{
if (isSprinting)
{
isSprint=false;
Speed = WalkSpeed;
}
else
{
isSprint=true;
Speed = RunSpeed;
}
}
}
A very similar issue is taking place with "Vscale". You'll want to create a public float and set it up like how sprint is setup in my code.
bool isCrouching; float Vscale;
void Update()
{
if (Input.GetButtonDown("Crouch"))
{
if (isCrouching)
{
isSprint=false;
Vscale= vScaleUp;
}
else
{
isCrouching=true;
Vscale= vScaleDown;
}
}
}
Your answer
Follow this Question
Related Questions
Want Slide down to shoot in android script Like Ballz Game 1 Answer
How would do I play a walking animation when i move my joystick? 0 Answers
MoveTowards is curving for no reason 0 Answers
Player pushes object but gets pushed into the Wall/Boundary 0 Answers
How to crouch a FPS Controller? 2 Answers