A
A
Alex_Kuzen2021-12-15 20:02:26
Unity
Alex_Kuzen, 2021-12-15 20:02:26

How to make a character slash in unity?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{
    public Rigidbody2D rb;
    [SerializeField]float speed = 0.10f;
    [SerializeField]float Jump_Force = 2f;
    [SerializeField]bool onGround = true;
    [SerializeField]bool force;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        var moveX = Input.GetAxis("Horizontal") * speed;
        transform.Translate(moveX, 0,0, Space.World);
        if (Input.GetKey(KeyCode.Space) && onGround == true)
        {
            rb.AddForce(Vector2.up * Jump_Force, ForceMode2D.Force);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift) & Input.GetKey(KeyCode.A) )
        {
            rb.AddForce(Vector2.left * 10, ForceMode2D.Impulse);
            


        }
        if(Input.GetKeyDown(KeyCode.LeftShift) & Input.GetKey(KeyCode.D))
            {
            rb.AddForce(Vector2.right * -10, ForceMode2D.Impulse);
            
            


        }
    }
    private void OnCollisionStay2D(Collision2D collision)
    {
        onGround = true;
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        onGround = false;
    }
}


Here in this place I want the character to sharply make a slash / roll to the side, with acceleration.
}
        if (Input.GetKeyDown(KeyCode.LeftShift) & Input.GetKey(KeyCode.A) )
        {
            rb.AddForce(Vector2.left * 10, ForceMode2D.Impulse);

But not only does he not make a sharp jerk, he also moves endlessly in a certain direction. How to make the jerk short?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nekit Medvedev, 2021-12-16
@NIKROTOS

var moveX = Input.GetAxis("Horizontal") * speed;// эту строку лучше написать в старт, или хотя-бы объявить переменную заранее, а не делать это каждый кадр)
transform.Translate(moveX, 0,0, Space.World);//вот здесь написано постоянно двигаться в одном направлении.

If you need him to constantly move forward and jump to the sides, continuing to run further (runner), then you should move the hero along the coordinates. To control the roll speed, it is better to use curves ( https://dtf.ru/gamedev/943158-alternativnye-methody... )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question