I wanna make a Movement Code for a Plataform 2D. I know that my code isn’t good, but works good for movement, jump and Double Jump.
I’m using RigidBody2D AddForce to Move and Jump, and using “Velocity X = 0” to remove acceleration.
This worked, but now I want to add Wall Jump and I can’t use AddForce or Velocity on X Axys cause the Velocity X is Constantly 0.
Can I do anything or I need to change all my movement System?
(Sorry my English)
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlaRe : MonoBehaviour {
public int velR, puloR, puloWall;
public Rigidbody2D rigidB;
public bool direiRe;
//Teste Bools
public bool dJump, wJump, bJump;
public static int maxJump;
void Start () {
maxJump = 0;
direiRe = true;
rigidB = gameObject.GetComponent<Rigidbody2D>();
}
void Update () {
//rigidB.velocity = new Vector2(0, rigidB.velocity.y);
// Movimentos
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
if (!ConfereChao.colidiP || !direiRe)
{
MoveRe(); direiRe = true;
}
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
if (!ConfereChao.colidiP || direiRe)
{
MoveRe(-1); direiRe = false;
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (ConfereChao.colidiC || (maxJump == 1 && dJump))
{
if (!ConfereChao.colidiC)
{
rigidB.velocity = new Vector2(rigidB.velocity.x, 0);
}
rigidB.AddForce(Vector2.up * puloR);
maxJump++;
}
if (ConfereChao.colidiP && wJump && !ConfereChao.colidiC)
{
rigidB.velocity = new Vector2(rigidB.velocity.x, 0);
rigidB.AddForce(Vector2.up * puloWall);
}
}
// Sprite Lado
if (direiRe == true)
{
GameObject.Find("TextuRe").transform.rotation = Quaternion.Euler(0, 0, 0);
}
else
{
GameObject.Find("TextuRe").transform.rotation = Quaternion.Euler(0, 180, 0);
}
}
void MoveRe(int leftRight = 1){
rigidB.AddForce(Vector2.right*velR*leftRight);
}
}