Hello, i have a problem and i can’t solve it alone.
Every time when i look down and press S
key, my player start jumping into the air, same thing when looking down and press W
key.
How can i solve it?
My code below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.XR.WSA.Input;
public class FPController : MonoBehaviour
{
public AudioSource weapon_reload;
public AudioSource death;
public AudioSource triggerSound;
public AudioSource pickAmmo;
public AudioSource pickMedKit;
public AudioSource land;
public AudioSource jump;
public AudioSource[] footsteps;
public Animator anim;
public GameObject cam;
float speed = 0.05f;
float Xsensitivity = 2;
float Ysensitivity = 2;
float MinX = -90;
float MaxX = 90;
Rigidbody rb;
CapsuleCollider capsule;
Quaternion cameraRot;
Quaternion characterRot;
bool cursorIsLocked = true;
bool lockCursor = true;
float x;
float z;
//Invetory
int ammo = 0;
int max_ammo = 50;
//Healt
int health = 0;
int max_health = 100;
int ammoClip = 0;
int ammoClipMax = 10;
bool playingWalking = false;
bool prevoiuslyGrounded = true;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody>();
capsule = this.GetComponent<CapsuleCollider>();
cameraRot = cam.transform.localRotation;
characterRot = this.transform.localRotation;
health = max_health;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
anim.SetBool("arm", !anim.GetBool("arm"));
anim.ResetTrigger("fire");
}
if (Input.GetMouseButtonDown(0) && !anim.GetBool("fire"))
{
if (ammoClip > 0)
{
anim.SetTrigger("fire");
ammoClip--;
}
else if (anim.GetBool("arm"))
{
triggerSound.Play();
}
Debug.Log("Ammo left in clip: " + ammoClip);
}
if (Input.GetKeyDown(KeyCode.R) && anim.GetBool("arm"))
{
int amountNeeded = ammoClipMax - ammoClip;
int ammoAvailable = amountNeeded < ammo ? amountNeeded : ammo;
ammo -= ammoAvailable;
ammoClip += ammoAvailable;
Debug.Log("Ammo left: " + ammo);
Debug.Log("Ammo in Clip: " + ammoClip);
Debug.Log("Ammo left: " + ammo);
anim.SetTrigger("reload-Trigger");
weapon_reload.Play();
}
if (Mathf.Abs(x) > 0 || Mathf.Abs(z) > 0)
{
if (!anim.GetBool("walking"))
{
anim.SetBool("walking", true);
InvokeRepeating("Play_foot_steps", 0, 0.4f);
}
}
else if (anim.GetBool("walking"))
{
anim.SetBool("walking", false);
CancelInvoke("Play_foot_steps");
playingWalking = false;
}
bool grounded = IsGorunded();
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
rb.AddForce(0, 250, 0);
jump.Play();
if (anim.GetBool("walking"))
{
playingWalking = false;
CancelInvoke("Play_foot_steps");
}
}
else if(!prevoiuslyGrounded && grounded)
{
land.Play();
}
prevoiuslyGrounded = grounded;
}
void Play_foot_steps()
{
AudioSource audioSource = new AudioSource();
int n = Random.Range(1, footsteps.Length);
audioSource = footsteps[n];
audioSource.Play();
footsteps[n] = footsteps[0];
footsteps[0] = audioSource;
playingWalking = true;
}
void FixedUpdate()
{
float yRot = Input.GetAxis("Mouse X") * Ysensitivity;
float xRot = Input.GetAxis("Mouse Y") * Xsensitivity;
cameraRot *= Quaternion.Euler(-xRot, 0, 0);
characterRot *= Quaternion.Euler(0, yRot, 0);
cameraRot = ClampRotationAroundXAxis(cameraRot);
this.transform.localRotation = characterRot;
cam.transform.localRotation = cameraRot;
x = Input.GetAxis("Horizontal") * speed;
z = Input.GetAxis("Vertical") * speed;
transform.position += cam.transform.forward * z + cam.transform.right * x;
UpdateCursorLock();
}
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
angleX = Mathf.Clamp(angleX, MinX, MaxX);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
return q;
}
bool IsGorunded()
{
RaycastHit hitInfo;
if (Physics.SphereCast(transform.position, capsule.radius, Vector3.down, out hitInfo,
(capsule.height / 2f) - capsule.radius + 0.1f))
{
return true;
}
return false;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Ammo" && ammo < max_ammo)
{
ammo = Mathf.Clamp(ammo + 10, 0, max_ammo);
Debug.Log("Ammo: " + ammo);
Destroy(col.gameObject);
pickAmmo.Play();
}
else if (col.gameObject.tag == "MedKit" && health < max_health)
{
health = Mathf.Clamp(health + 10, 0, max_health);
Debug.Log("Healt: " + health);
Destroy(col.gameObject);
pickMedKit.Play();
}
else if (col.gameObject.tag == "Lava")
{
health = Mathf.Clamp(health - 25, 0, max_health);
Debug.Log("Healt left: " + health);
if(health <= 0)
{
death.Play();
}
}
else if (IsGorunded())
{
if (anim.GetBool("walking") && !playingWalking)
InvokeRepeating("Play_foot_steps", 0, 0.4f);
}
}
public void SetCursorLock(bool value)
{
lockCursor = value;
if (!lockCursor)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
void UpdateCursorLock()
{
if (lockCursor)
InternalLockUpdate();
}
void InternalLockUpdate()
{
if(Input.GetKeyUp(KeyCode.Escape))
cursorIsLocked = false;
else if(Input.GetMouseButtonUp(0))
cursorIsLocked = true;
if(cursorIsLocked)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else if (!cursorIsLocked)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
}
Thanks.