You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.5 KiB
56 lines
1.5 KiB
using System;
|
|
using UnityEngine;
|
|
|
|
public class PlayerMove : MonoBehaviour
|
|
{
|
|
[SerializeField] private float speed;
|
|
[SerializeField] private Rigidbody rb;
|
|
[SerializeField] private Vector3 StartPos;
|
|
|
|
bool vertical;
|
|
bool horizontal;
|
|
private void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float horizontalInput = Input.GetAxis("Horizontal");
|
|
float verticalInput = Input.GetAxis("Vertical");
|
|
horizontalInput = Mathf.Round(horizontalInput);
|
|
verticalInput = Mathf.Round(verticalInput);
|
|
|
|
Vector3 moveDirection;
|
|
|
|
if (horizontalInput != 0 && horizontal == true)
|
|
{
|
|
vertical = false;
|
|
moveDirection = new Vector3(horizontalInput,0, 0);
|
|
moveDirection.Normalize();
|
|
|
|
}
|
|
else if (verticalInput != 0 && vertical == true)
|
|
{
|
|
horizontal = false;
|
|
moveDirection = new Vector3(0, 0, verticalInput);
|
|
moveDirection.Normalize();
|
|
}
|
|
else
|
|
{
|
|
moveDirection = new Vector3(0, 0, 0);
|
|
horizontal = true;
|
|
vertical = true;
|
|
}
|
|
|
|
|
|
|
|
transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
this.transform.position = StartPos;
|
|
}
|
|
}
|