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.
 
 
 
 

81 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
public GameObject model;
private Vector2 receivedInput;
// Start is called before the first frame update
void Start()
{
StartCoroutine((FlipOnAxis()));
}
public void SetMovement(Vector2 input)
{
receivedInput = input;
}
public void UpdatePosition()
{
float HorseX, HorseZ;
HorseZ = Input.GetAxisRaw("Vertical");
HorseX = Input.GetAxisRaw("Horizontal");
float rotateTo = RotateObject(HorseX, HorseZ);
HorseZ = Input.GetAxis("Vertical") * Time.deltaTime * walkSpeed;
HorseX = Input.GetAxis("Horizontal") * Time.deltaTime * walkSpeed;
transform.Translate(HorseX, 0, HorseZ);
LeanTween.rotateY(model, rotateTo, 0.1f);
}
public float RotateObject(float xInput, float zInput)
{
float to = model.transform.rotation.eulerAngles.y;
//Debug.Log(to);
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
if (zInput > 0 && xInput == 0)
to = 0;
else if (zInput < 0 && xInput == 0)
to = 180;
else if (zInput == 0 && xInput > 0)
to = 90;
else if (zInput == 0 && xInput < 0)
to = 270;
else if (zInput > 0 && xInput > 0)
to = 45;
else if (zInput < 0 && xInput > 0)
to = 135;
else if (zInput < 0 && xInput < 0)
to = 225;
else if (zInput > 0 && xInput < 0)
to = 315;
}
return to;
}
public IEnumerator FlipOnAxis()
{
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
LeanTween.rotateX(model, -90, 0.1f);
}
yield return new WaitForSeconds(0.5f);
}
}
// Update is called once per frame
void Update()
{
UpdatePosition();
}
}