CoronaRun/Assets/Scenes/scripts/player.cs
2025-01-19 22:16:28 +05:30

52 lines
1.1 KiB
C#

using System.IO;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class player : MonoBehaviour
{
public float playerSpeed;
private Rigidbody2D rb;
private Vector2 playerDirection;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
float directionY;
void Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
OnupButton();
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
ondownbutton();
}
if(Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow) )
{
OnbuttonRelease();
}
playerDirection = new Vector2(0, directionY).normalized;
}
void FixedUpdate()
{
rb.linearVelocity = new Vector2(0, playerDirection.y*playerSpeed);
}
public void ondownbutton()
{
directionY = -1;
}
public void OnbuttonRelease()
{
directionY = 0;
}
public void OnupButton()
{
directionY = 1;
}
}