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

44 lines
1.1 KiB
C#
Raw Normal View History

2021-09-24 00:34:18 -06:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement2D : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 10f;
public bool isGrounded;
2021-10-18 02:17:36 -06:00
public AudioSource jumpSound;
public AudioSource deathSound;
2021-09-24 00:34:18 -06:00
private SpriteRenderer sprite;
public void Start()
{
sprite = gameObject.GetComponent<SpriteRenderer>();
}
private void Update()
{
Jump();
2021-10-07 13:56:22 -06:00
Vector3 movement = new Vector3(Input.GetAxis("platformerHorizontal"), 0f, 0f);
2021-09-24 00:34:18 -06:00
transform.position += movement * Time.deltaTime * moveSpeed;
2021-10-07 13:56:22 -06:00
if (Input.GetAxis("platformerHorizontal") > 0)
2021-09-24 00:34:18 -06:00
{
2021-10-14 13:35:35 -06:00
sprite.flipX = false;
2021-09-24 00:34:18 -06:00
}
2021-10-07 13:56:22 -06:00
else if (Input.GetAxis("platformerHorizontal") < 0)
2021-09-24 00:34:18 -06:00
{
2021-10-14 13:35:35 -06:00
sprite.flipX = true;
2021-09-24 00:34:18 -06:00
}
}
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
gameObject.GetComponent<Rigidbody2D>().velocity = (new Vector2(0f, jumpForce));
2021-10-18 02:17:36 -06:00
jumpSound.Play();
2021-09-24 00:34:18 -06:00
}
}
}