Help with this controls code, Can i get some help with this character controller
Im making this character control code but i made from parts of other codes, is there a more effective way of doing it?
using UnityEngine;
using System.Collections;
public class Ch1 : MonoBehaviour {
public Vector2 JumpF;
public float MaxSpeed;
private bool MoveL;
private bool MoveR;
private bool jump;
private bool IsGrounded = false;
public KeyCode Left;
public KeyCode Right;
public KeyCode Jump;
public Rigidbody2D Rb;
void Start (){
Rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate () {
if (MoveR == true) {
Rb.AddForce (new Vector2 (10000 * Time.deltaTime, 0));
} else if (MoveL == true) {
Rb.AddForce (new Vector2 (-10000 * Time.deltaTime, 0));
}
if ( (IsGrounded == true) && (jump == true)) {
Rb.MovePosition(Rb.position + JumpF *Time.fixedDeltaTime);
//Rb.AddForce (new Vector2 (0, 1000));
}
if ( Rb.velocity.x > MaxSpeed ){
Rb.velocity = new Vector2 (MaxSpeed, Rb.velocity.y);
}
if ( Rb.velocity.x < -MaxSpeed){
Rb.velocity = new Vector2 (-MaxSpeed, Rb.velocity.y);
}
}
void Update () {
if (Input.GetKey (Right)) {
MoveR = true;
} else if (Input.GetKey (Left)) {
MoveL = true;
}else {
MoveR=false;
MoveL=false;}
if (Input.GetKeyDown (Jump)) {
jump=true;
}else {
jump=false;}
}
void OnCollisionEnter2D (Collision2D Col){
if (Col.gameObject.name == "floor"){
IsGrounded = true;
}
}
void OnCollisionExit2D (Collision2D Col){
if (Col.gameObject.name == "floor"){
IsGrounded = false;
}
}
}
Answer by Cuttlas-U · Mar 01, 2018 at 09:06 AM
hey; u did it all good i cant see any problems ; and dont be so sensitive u dont need to be 100 optimized;
the only problem in your script is u can use CompareTag
for fast results ;
like this :
void OnCollisionEnter2D (Collision2D Col){
if (Col.gameObject.CompareTag("floor")){ // means true and the tag is floor
IsGrounded = true;
}
}
Answer by vinilly · Apr 25, 2018 at 06:43 AM
I hope this helps I tried to clean it up some...
Usually I wont separate the inputs from the controllers. But this is sort of easier on the eyes but confusing someone who may be new to C#.
using UnityEngine;
using System.Collections;
public class Ch1 : MonoBehaviour
{
//Logic
public float MaxSpeed;
private bool MoveL;
private bool MoveR;
private bool jump;
private bool IsGrounded = false;
//Variables
public Vector2 JumpF;
public Rigidbody2D Rb;
//Initiallize
void Start ()
{
//Access Rigidbody2D Component
Rb = GetComponent<Rigidbody2D>();
}
//Between Update and LateUpdate frame.
void FixedUpdate ()
{
PlayerMovement();
}
void PlayerMovement()
{
//Moving Player Inputs
if (MoveR == true)
{
Rb.AddForce(new Vector2(10000 * Time.deltaTime, 0));
transform.localScale = new Vector3(1f, 1f, 1f); //Make the GameObject face right
}
if (MoveL == true)
{
Rb.AddForce(new Vector2(-10000 * Time.deltaTime, 0));
transform.localScale = new Vector3(-1f, 1f, 1f); //Make the GameObject face left
}
//Jumping Player Inputs
if (jump == true)
{
Rb.MovePosition(Rb.position + JumpF * Time.fixedDeltaTime);
//Rb.AddForce (new Vector2 (0, 1000));
}
if (Rb.velocity.x > MaxSpeed)
{
Rb.velocity = new Vector2(MaxSpeed, Rb.velocity.y);
}
if (Rb.velocity.x < -MaxSpeed)
{
Rb.velocity = new Vector2(-MaxSpeed, Rb.velocity.y);
}
}
//Update at the start of per frame
void Update ()
{
PlayerInputControl();
}
void PlayerInputControl()
{
if (Input.GetKey(KeyCode.D))
{
MoveR = true;
MoveL = false;
}
if (Input.GetKey(KeyCode.A))
{
MoveL = true;
MoveR = false;
}
else
{
MoveR = false;
MoveL = false;
}
if (Input.GetButtonDown("Jump") && IsGrounded)
{
jump = true;
}
else
{
jump = false;
}
}
//Collision On Enter
void OnCollisionEnter2D (Collision2D Col)
{
if (Col.gameObject.name == "floor")
{
IsGrounded = true;
}
}
//Collision On exit
void OnCollisionExit2D (Collision2D Col)
{
if (Col.gameObject.name == "floor")
{
IsGrounded = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612153556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to double my character's movement speed whenever spacebar is pressed? 0 Answers
Character flies up and gets stuck there, won't move forward? 1 Answer
Normalized Vector3 Movement Really Slow? 2 Answers
Using gravity, CharacterController.isGrounded still is unreliable 0 Answers
Are Coroutines the best way to delay ? If not, what is ? C# 2 Answers