Question by
Centriax · Jul 08, 2020 at 12:52 PM ·
2dplatformercrouching
Wanted this code to force player to crouch when an object is above, but instead character seems to be stuck on crouch
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Animations;
using UnityEngine;
using System.Net.Sockets;
using JetBrains.Annotations;
using System.Xml.Serialization;
public class PlayerController2D : MonoBehaviour
{
//Set-up
//Variables
public float jumpPower;
public float sidewaysPower;
bool isGrounded;
bool isCrouched;
public float crouchingSidewayPower;
private int extraJumps;
const float CeilingRadius = .2f;
private bool crouch;
//References
[SerializeField]Transform groundCheck;
public Transform groundCheckR;
public Transform groundCheckL;
[SerializeField] private LayerMask WhatIsGround;
[SerializeField] private Transform CeilingCheck;
public Rigidbody2D rb2D;
public Animator animator;
public SpriteRenderer sprite2D;
public BoxCollider2D standingCollider;
public BoxCollider2D crouchingCollider;
//Reference intialized
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite2D = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
//Ground Check
if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))
{
isGrounded = true;
}
else
{
isGrounded = false;
animator.Play("Jumping");
}
//Side-Movement
if (Input.GetKey("a") && isCrouched == false)
{
if (isGrounded == true)
{
rb2D.velocity = new Vector2(-sidewaysPower, rb2D.velocity.y);
}
else
{
rb2D.velocity = new Vector2(-sidewaysPower, rb2D.velocity.y);
}
if (isGrounded == true)
animator.Play("Moving");
sprite2D.flipX = true;
}
else if (Input.GetKey("d") && isCrouched == false)
{
if (isGrounded == true)
{
rb2D.velocity = new Vector2(sidewaysPower, rb2D.velocity.y);
}
else
{
rb2D.velocity = new Vector2(sidewaysPower, rb2D.velocity.y);
}
if (isGrounded == true)
animator.Play("Moving");
sprite2D.flipX = false;
}
else
{
if (isGrounded == true && isCrouched == false)
{
animator.Play("Idle");
rb2D.velocity = new Vector2(0, rb2D.velocity.y);
}
}
}
//Jump and Double Jump Function
private void Update()
{
if (isGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && isCrouched == false)
{
rb2D.velocity = Vector2.up * jumpPower;
extraJumps--;
Debug.Log("now!");
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true && isCrouched == false)
{
rb2D.velocity = Vector2.up * jumpPower;
Debug.Log("now!");
}
//Crouch Function
if (isGrounded == true)
{
if(Input.GetKey(KeyCode.LeftControl) || crouch == true)
{
isCrouched = true;
standingCollider.enabled = false;
if (Input.GetKey(KeyCode.A))
{
animator.Play("CrouchingMoving");
rb2D.velocity = new Vector2(-crouchingSidewayPower, 0);
sprite2D.flipX = true;
}
else if (Input.GetKey(KeyCode.D))
{
rb2D.velocity = new Vector2(crouchingSidewayPower, 0);
animator.Play("CrouchingMoving");
sprite2D.flipX = false;
}
else
{
animator.Play("IdleCrouching");
rb2D.velocity = new Vector2(0, 0);
}
}
else
{
isGrounded = false;
standingCollider.enabled = false;
}
}
else
{
}
if (Physics2D.OverlapCircle(CeilingCheck.position, CeilingRadius, WhatIsGround))
{
crouch = true;
}
else
{
crouch = false;
}
}
}
Comment