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.
 
 
 
 

151 lines
4.2 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
public float gravity;
public GameObject model;
public CharacterController cController;
public Camera cam;
public HerdController herd;
public float speedMulitplier;
private float randomizer;
private float directionRandmoizer;
private Vector2 receivedInput;
private Vector3 moveDirection = Vector3.zero;
private float moveDelta;
private float lastMoveTime;
// Start is called before the first frame update
void Start()
{
speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f);
cController = GetComponent<CharacterController>();
randomizer = UnityEngine.Random.Range(0,10);
cam = FindObjectOfType<Camera>();
herd = FindObjectOfType<HerdController>();
}
public void SetMovement(Vector2 input)
{
receivedInput = input;
}
public void UpdatePosition()
{
moveDelta = Time.time - lastMoveTime;
lastMoveTime = Time.time;
float HorseX, HorseZ;
HorseZ = receivedInput.y;
HorseX = receivedInput.x;
if (receivedInput.magnitude != 0)
{
if (Mathf.Abs(HorseZ) == Mathf.Min(Mathf.Abs(HorseZ), Mathf.Abs(HorseX)))
{
HorseZ += directionRandmoizer;
}
else
{
HorseX += directionRandmoizer;
}
}
if (cController.isGrounded)
{
moveDirection = new Vector3(HorseX, 0, HorseZ);
moveDirection *= walkSpeed * speedMulitplier;
}
moveDirection.y -= gravity * Time.deltaTime;
cController.Move(moveDirection * Time.deltaTime);
//float rotateTo = RotateObject(HorseX, HorseZ);
//HorseZ *= Time.deltaTime * walkSpeed * speedMulitplier;
//HorseX *= Time.deltaTime * walkSpeed * speedMulitplier;
//
//
//transform.Translate(HorseX, 0, HorseZ);
//Vector3 dir = Quaternion.Euler(-90, rotateTo, 0) * Vector3.forward;
//model.transform.forward = dir;
//LeanTween.rotateY(model, rotateTo, 0.1f);
}
public float RotateObject(float xInput, float zInput)
{
float to = model.transform.rotation.eulerAngles.y;
//Debug.Log(to);
if (receivedInput.x != 0 || receivedInput.y != 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 void MoveObject(float wait)
{
StartCoroutine(RandomWait(wait));
}
public IEnumerator RandomWait(float wait)
{
yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait/2));
if (cController.isGrounded)
{
Vector3 rotateDir = new Vector3(90 * Math.Sign(receivedInput.y), 0, -90 * Math.Sign(receivedInput.x));
model.transform.Rotate(rotateDir, Space.World);
}
}
public void DestroyOffCamera()
{
Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
if (viewPos.y < -0.1)
herd.RemoveHorse(this);
}
// Update is called once per frame
void Update()
{
speedMulitplier = (Mathf.Sin(Time.time * UnityEngine.Random.Range(0.9f,1.1f) + randomizer) + 2);
directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.3f;
UpdatePosition();
DestroyOffCamera();
}
public void AddForce(Vector3 direction)
{
moveDirection += direction;
transform.Translate(direction*Time.deltaTime);
}
}