Answer the question
In order to leave comments, you need to log in
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;
}
}
}
if (Input.GetKeyDown(KeyCode.LeftShift) & Input.GetKey(KeyCode.A) )
{
rb.AddForce(Vector2.left * 10, ForceMode2D.Impulse);
Answer the question
In order to leave comments, you need to log in
var moveX = Input.GetAxis("Horizontal") * speed;// эту строку лучше написать в старт, или хотя-бы объявить переменную заранее, а не делать это каждый кадр)
transform.Translate(moveX, 0,0, Space.World);//вот здесь написано постоянно двигаться в одном направлении.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question