Can someone give me some script that makes the player move both horizontal axis and vertical axis?
I took this game as a reference of the axes I want to create;
Can someone give me some script that makes the player move both horizontal axis and vertical axis?
I took this game as a reference of the axes I want to create;
It is probably a good idea to check some tutorials. You will need to write some code eventually anyway.
Have you checked movement basics tutorial?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float movespeed;
private bool isMoving;
private Vector2 input;
private void Update()
{
if (!ismoving)
{
input.x = Input.GetAxisRaw(“Horizontal”);
input.y = Input.GetAxisRaw(“Vertical”);
if (input.x != 0) inpuy.y = 0;
if (input!= Vector2.zero)
{
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;
StartCoroutine(Move(targetPos));
}
}
}
IEnumerator Move(Vector3 targetPos)
{
isMoving = true;
while((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, movePos, movespeed * Time.deltaTime);
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
}