Ruch Unity 2d
// This movement is used for platformer type games
bool jump = false;
float MovementSmoothness = 0.1f;
public float JumpForce;
public PlayerSpeed;
Rigidbody2D rb;
void Start(){
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if(Input.GetKeyDown(KeyCode.W) jump = true;
}
private void FixedUpdate()
{
if(jump){
rb.AddForce(new Vector2(0f, JumpForce));
jump = false;
}
if(Input.GetKey(KeyCode.LeftShift))// If shift is pressed, sprint
targetvelocity = new Vector2(horizontal * PlayerSpeed * 2 * Time.deltaTime, rb.velocity.y);
else // else go normally
targetvelocity = new Vector2(horizonal * PlayerSpeed * Time.deltaTime, rb.velocity.y);
// this makes movement much smoother ans satisfying
rb.velocity = Vector3.SmoothDamp(rb.velocity,targetvelocity, new Vector3(0,0,0), MovementSmoothness);
}
Xixika