S
S
samo4ek2018-05-16 20:50:22
2D
samo4ek, 2018-05-16 20:50:22

How can you optimize?

Made a jump from wall to wall (wall jump). I puzzled over how to do it for a long time, but in the end it turned out this.
Tell me how to optimize the code

public class noobscript : MonoBehaviour
 private bool iswallright = false;
    private bool iswallleft = false;
    private bool iswallrightflip = false;
    private bool iswallleftflip = false;
public LayerMask wallright;
    public LayerMask wallleft;
    public Transform rightwall;
    public Transform leftwall;
    public Transform rightwallflip;
    public Transform leftwallflip;
private void FixedUpdate()
iswallright = Physics2D.OverlapCircle(rightwall.position, groundRadius, wallright);
        iswallleft = Physics2D.OverlapCircle(leftwall.position, groundRadius, wallleft);
        iswallrightflip = Physics2D.OverlapCircle(rightwallflip.position, groundRadius, wallright);
        iswallleftflip = Physics2D.OverlapCircle(leftwallflip.position, groundRadius, wallleft);
private void Update()
 if (iswallright && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(new Vector2(-300, 600));
            Flip();
            
            
        }

        if (iswallleft && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(new Vector2(300, 600));
            Flip();
          
        }
        if (iswallrightflip && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(new Vector2(-300, 600));
            Flip();


        }

        if (iswallleftflip && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(new Vector2(300, 600));
            Flip();

        }
 private void Flip()
    {
     
        isFacingRight = !isFacingRight;
   
        Vector3 theScale = transform.localScale;
 
        theScale.x *= -1;
    
        transform.localScale = theScale;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ark Tarusov, 2018-05-30
@samo4ek

Why do you need to know the walls?
Just stick the player on the wall.
If, after hitting the wall, he makes a jump, then just throw it in the opposite direction, and it’s not your concern if it hits.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question