P
P
Pythonchik32020-05-09 17:34:38
C++ / C#
Pythonchik3, 2020-05-09 17:34:38

Why doesn't AddForce work?

There is this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float speed = 4.5f;
    public float Climbspeed = 1.0f;
    public float jumpForce = 8.0f;

    private CapsuleCollider2D _box;
    private Rigidbody2D _body;
    private Animator _anim;

    private bool grounded = false;
    private bool jump = false;
    private bool stairs = false;
    private float Crouch;

    private string[] TagNames = { "floor", "Stairs", "Untagged" };
    private const int TGLen = 3;
    private int AllTags;
    private Dictionary<string, int> Bricks = new Dictionary<string, int>(TGLen);

    void Start()
    {
        _body = GetComponent<Rigidbody2D>();
        _anim = GetComponent<Animator>();
        _box = GetComponent<CapsuleCollider2D>();

        for (int i = 0; i < TGLen; i++)
        {
            Bricks.Add(TagNames[i], 0);
        }
    }

    void Update()
    {
        float deltaX = Input.GetAxis("Horizontal") * speed;
        float deltaY = Input.GetAxis("Vertical") * Climbspeed;
        float Crouch = Input.GetAxis("Fire1");

        AllTags = GetTagSum();
        grounded = Bricks["floor"] > 0;
        stairs = Bricks["Stairs"] > 0;
        if (grounded && Input.GetKeyDown(KeyCode.Space) && Crouch == 0)
        {
            _body.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
            jump = true;
            Debug.Log('!');
        }
        if (grounded)
        {
            jump = false;
        }
        if (AllTags == 0) { jump = true; }
        if (jump) { Crouch = 0; }
        if (stairs && !jump &&false)
        {
            Crouch = 0;
            _body.gravityScale = 0;
        }
        else
        {
            deltaY = 0;
            _body.gravityScale = 3;
        }
        if (!jump)
        {
            Vector2 movement = new Vector2(deltaX * (1 - Crouch), deltaY);
            _body.velocity = movement;
        }
        else
        {
            Vector2 movement = new Vector2(deltaX * (1 - Crouch), _body.velocity.y);
            _body.velocity = movement;
        }


        {
            _anim.SetBool("Go", deltaX != 0 && grounded);
            _anim.SetBool("Jump", jump);
            _anim.SetBool("Crouch", Crouch == 1);
            _anim.SetBool("Climb", stairs);
            _anim.SetBool("ClimbGo", deltaY != 0);
        }

        if (!Mathf.Approximately(deltaX, 0) && !stairs)
        {
            transform.localScale = new Vector3(Mathf.Sign(deltaX), 1, 1);
        }
    }

    int GetTagSum()
    {
        int sum = 0;
        for(int i = 0; i < TGLen; i++)
        {
            sum += Bricks[TagNames[i]];
        }
        return sum;
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Bricks[other.gameObject.tag]++;
    }

    void OnTriggerExit2D(Collider2D other)
    {
        Bricks[other.gameObject.tag]--;
    }
}

For some reason, AddForce works every other time. ! is displayed in the console, i.e. pressing happened, force was applied, but for some reason the character does not jump. Where is my mistake? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dyupachev, 2020-05-09
@Pythonchik3

Working with physics in update is a bad idea. Try it through fixedupdate, this will at least solve the problem of various lags in the future, and perhaps specifically yours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question