using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float speed = 5f;
private float mouseX;
private float jumpVelocity = 5f;
public float distanceToGround = 0.1f;
public LayerMask groundLayer;
public GameObject bullet;
private float bulletSpeed = 100f;
private Rigidbody rb;
private CapsuleCollider coll;
void Start()
{
rb = GetComponent<Rigidbody>();
coll = GetComponent<CapsuleCollider>();
}
void FixedUpdate()
{
if (isGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpVelocity, ForceMode.Impulse);
print("Jump");
}
MovementLogic();
if (Input.GetMouseButtonDown(0))
{
GameObject newBullet = Instantiate(bullet,
transform.position + new Vector3(1,0,0),
transform.rotation) as GameObject;
Rigidbody bulletRb = newBullet.GetComponent<Rigidbody>();
bulletRb.velocity = transform.forward * bulletSpeed;
}
}
private void LateUpdate()
{
RotateLogic();
}
private void RotateLogic()
{
mouseX = Input.GetAxis("Mouse X") * 2;
transform.Rotate(mouseX * new Vector3(0, 1, 0));
}
private void MovementLogic()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement * speed * Time.fixedDeltaTime);
}
private bool isGrounded()
{
Vector3 capsuleBottom = new Vector3(coll.bounds.center.x,
coll.bounds.min.y, coll.bounds.center.z);
bool grounded = Physics.CheckCapsule(coll.bounds.center, capsuleBottom,distanceToGround,groundLayer,QueryTriggerInteraction.Ignore);
return grounded;
}
}
My project is not very heavy and mayby the problem is not in optimizations.
But im waiting youre suggestions