- Home /
Question by
Alphacreations · Apr 27, 2020 at 04:13 AM ·
cheat
Unity cheatcode not working
This is my code for my character controller in unity and I have implemented a cheat code where when you type in "speed" you go faster. The problem is that whenever I enter the cheat code, I am not able to jump afterwards
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
private Animator anim;
private enum State { idle, running, jumping, falling }
private State state = State.idle;
private Collider2D coll;
[SerializeField] private LayerMask ground;
[SerializeField] private float speed = 8f;
[SerializeField] private float JumpForce = 15f;
private string[] cheatCode;
private int index;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
coll = GetComponent<Collider2D>();
cheatCode = new string[] { "s", "p", "e", "e", "d" };
index = 0;
}
private void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKeyDown(cheatCode[index]))
{
index++;
}
else
{
index = 0;
}
}
if (index == cheatCode.Length)
{
speed = 30f;
}
float hDirection = Input.GetAxis("Horizontal");
if (hDirection < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
else if (hDirection > 0)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
transform.localScale = new Vector2(1, 1);
}
else
{
}
if (Input.GetButtonDown("Jump") && coll.IsTouchingLayers())
{
rb.velocity = new Vector2(rb.velocity.x, JumpForce);
state = State.jumping;
}
VelocityState();
anim.SetInteger("state", (int)state);
void VelocityState()
{
if (state == State.jumping)
{
if (rb.velocity.y < .1f)
{
state = State.falling;
}
}
else if (state == State.falling)
{
if (coll.IsTouchingLayers(ground))
{
state = State.idle;
}
}
else if (Mathf.Abs(rb.velocity.x) > 2f)
{
//Moving
state = State.running;
}
else
{
state = State.idle;
}
}
}
}
]
Comment
Your answer

Follow this Question
Related Questions
Creating realtime shadows without unity pro? 1 Answer
playerprefs save date cheat problem 1 Answer
How to implement cheat codes 8 Answers
Open scene by typing a word 2 Answers
making a cheat console? 3 Answers