gimm-platformer-project/Assets/Scripts/Movement2D.cs

43 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement2D : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 10f;
public bool isGrounded;
public AudioSource jumpSound;
public AudioSource deathSound;
private SpriteRenderer sprite;
public void Start()
{
sprite = gameObject.GetComponent<SpriteRenderer>();
}
private void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("platformerHorizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
if (Input.GetAxis("platformerHorizontal") > 0)
{
sprite.flipX = false;
}
else if (Input.GetAxis("platformerHorizontal") < 0)
{
sprite.flipX = true;
}
}
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
gameObject.GetComponent<Rigidbody2D>().velocity = (new Vector2(0f, jumpForce));
jumpSound.Play();
}
}
}