using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementController : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private float normalspeed;
[SerializeField] private float jumpForce;
[SerializeField] private float nextTimeToStrafe = 0;
[SerializeField] private float strafeRate = 2f;
[SerializeField] public bool onGround = true;
[SerializeField] private const int MAX_Jump = 2;
[SerializeField] private float fuel = 100f;
[SerializeField] private float fuelcost = 20;
[SerializeField] private float fuelrechargeamount = 5f;
[SerializeField] private float rechargeTimer = 2f;
[SerializeField] private float rechargeTime = 2f;
[SerializeField] private bool walljump = false;
[SerializeField] private float thrust = 2f;
[SerializeField] private float actionsdelay;
[SerializeField] private float actiondelayTimer;
[SerializeField] private float dash = 5;
[SerializeField] public bool iscrouching;
[SerializeField] public float characterheight = 2f;
[SerializeField] Animator animator;
[SerializeField] public float crouchheight = 1.4f;
public float hAxis;
public float vAxis;
public bool ismoving;
public bool ismage;
public float hvalue;
public float currenthealth;
[Header("Fall Damage")]
public float startYPos;
public float endYPos;
public float fallthreshold = 5;
public float falldamage;
public Rigidbody rb;
private CapsuleCollider cs;
[Header("OtherScripts")]
public PlayerVitals vs;
private void Start()
{
speed = 3f;
rb = GetComponent<Rigidbody>();
cs = GetComponent<CapsuleCollider>();
iscrouching = false;
animator = gameObject.GetComponent<Animator>();
}
private void Update()
{
Jump();
Dash();
Crouch();
Sprint();
Fuel();
Thrust();
}
private void Sprint()
{
if(Input.GetKey(KeyCode.LeftShift))
{
speed = 6;
animator.SetFloat("speed", speed);
}
else
{
speed = 3;
animator.SetFloat("speed", speed);
}
}
public void Falldamage()
{
if(!onGround)
{
falldamage = startYPos - endYPos;
if (transform.position.y < startYPos)
{
endYPos = transform.position.y;
if(endYPos < startYPos)
{
if(startYPos - endYPos > fallthreshold)
{
vs.maxHealth -= falldamage;
}
}
}
}
if(onGround)
{
startYPos = transform.position.y;
}
}
private void Crouch()
{
if (Input.GetKey("c"))
{
cs.height = .9f;
iscrouching = true;
}
else
{
}
}
private void Delay()
{
if(actiondelayTimer > 0)
{
actiondelayTimer -= Time.deltaTime;
}
if(actiondelayTimer <= 0)
{
actiondelayTimer = actionsdelay;
}
}
private void Dash()
{
if ((Input.GetKeyDown(KeyCode.LeftShift)) && iscrouching == true)
{
rb.AddForce(0, 0, dash, ForceMode.Impulse);
}
if (Input.GetKeyDown("x"))
{
cs.height = crouchheight;
rb.AddForce(0, 0, -dash, ForceMode.Impulse);
}
else
{
cs.height = characterheight;
}
}
private void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && onGround)
{
animator.SetTrigger("jump");
rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
onGround = false;
}
else
{
}
}
void OnCollisionEnter(Collision Collision)
{
if(Collision.gameObject.tag == "Ground")
{
onGround = true;
}
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
animator.SetFloat("hAxis", hAxis);
animator.SetFloat("vAxis", vAxis);
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);
Quaternion rotation = Quaternion.Euler(movement);
rb.MovePosition(newPosition);
rb.rotation = Quaternion.identity;
}
private void Fuel()
{
if (rechargeTimer > 0)
{
rechargeTimer -= Time.deltaTime;
}
if (rechargeTimer <= 0)
{
fuel += fuelrechargeamount;
rechargeTimer = rechargeTime;
}
}
private void Thrust()
{
if (Input.GetKeyDown("j") && fuel >= 20)
{
rb.AddForce(0, thrust, 0, ForceMode.Impulse);
fuel -= fuelcost;
}
if (Input.GetKeyDown("q") && fuel >= 20)
{
animator.SetTrigger("roll left");
rb.AddRelativeForce(-Vector3.right * thrust);
fuel -= fuelcost;
}
if (Input.GetKeyDown("e") && fuel >= 20)
{
animator.SetTrigger("roll right");
rb.AddRelativeForce(Vector3.right * thrust);
fuel -= fuelcost;
}
}
}
My thrust function moves my character perfectly on play but after moving to different angles. It screws everything up. Also my jump works perfectly but thrust once again takes like 2000+ to move my character decently. It is just gravity or a settings issue on my end.