Hi,
I’m having a big physics bug. When I play my game and presses C on the keyboard to make my player crouch, my player spins around himself and move in different directions. Does anyone have a solution to my crouch/physics problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public Transform PlayerHeight;
public float normalHeight, crouchHeight;
public float horizontalInput;
public float verticalInput;
public float speed = 3;
public float jump = 10;
public float climb = 5;
private bool isGrounded = false;
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
// Crouch
if (Input.GetKeyDown(KeyCode.C))
{
PlayerHeight.localScale = new Vector3 (0, crouchHeight, 0);
}
if (Input.GetKeyUp(KeyCode.C))
{
PlayerHeight.localScale = new Vector3(0, normalHeight, 0);
PlayerHeight.position += Vector3.up * (normalHeight / 2);
}
// Jump
if (transform.position.y <= 0.3)
{
isGrounded = true;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
isGrounded = false;
}
}
// Climb
private void OnCollisionEnter(Collision collision)
{
// Climb
if (Input.GetKeyDown(KeyCode.F) && collision.gameObject.CompareTag("Wall"))
{
transform.position += Vector3.up * climb * Time.deltaTime;
}
}
private void FixedUpdate()
{
Moveing();
}
public void Moveing()
{
if (Mathf.Abs(verticalInput) > 0 || Mathf.Abs(horizontalInput) > 0)
{
transform.Translate(Vector3.forward * Time.deltaTime * verticalInput);
transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
}
}
}