123 lines
3.3 KiB
C#
123 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class FirstPlayerController : MonoBehaviour
|
|
{
|
|
public float speed = 2.0f;
|
|
public float kickStrength = 0.001f;
|
|
public GameObject ball;
|
|
private Rigidbody rb;
|
|
private bool hasTheBall = false;
|
|
private Animator animator;
|
|
public GameObject mainCamera;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
animator = GetComponent<Animator>();
|
|
if (ball)
|
|
{
|
|
rb = ball.GetComponent<Rigidbody>();
|
|
}
|
|
}
|
|
|
|
void OnCollisionEnter(Collision collision)
|
|
{
|
|
Collider ballCollider = ball.GetComponent<Collider>();
|
|
if (collision.collider == ballCollider)
|
|
{
|
|
hasTheBall = true;
|
|
rb.isKinematic = true;
|
|
rb.useGravity = false;
|
|
}
|
|
}
|
|
|
|
void OnCollisionExit(Collision collision)
|
|
{
|
|
Collider ballCollider = ball.GetComponent<Collider>();
|
|
if (collision.collider == ballCollider)
|
|
{
|
|
hasTheBall = false;
|
|
rb.isKinematic = false;
|
|
rb.useGravity = true;
|
|
}
|
|
}
|
|
|
|
void ChangeCamera(float distance)
|
|
{
|
|
CameraMovement cm = this.mainCamera.GetComponent<CameraMovement>();
|
|
if (distance <= 0.1f)
|
|
{
|
|
cm.myPlayer = this.gameObject.transform;
|
|
print("prati covjeka");
|
|
}
|
|
else
|
|
{
|
|
print("prati loptu!");
|
|
cm.myPlayer = ball.transform;
|
|
}
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
float distanceToBall = Vector3.Distance(transform.position, ball.transform.position);
|
|
ChangeCamera(distanceToBall);
|
|
|
|
Vector3 moveDirection = Vector3.zero;
|
|
|
|
if (Input.GetKey(KeyCode.RightArrow))
|
|
{
|
|
moveDirection = Vector3.right * speed * Time.deltaTime;
|
|
transform.position += moveDirection;
|
|
}
|
|
if (Input.GetKey(KeyCode.LeftArrow))
|
|
{
|
|
moveDirection = Vector3.left * speed * Time.deltaTime;
|
|
transform.position += moveDirection;
|
|
}
|
|
if (Input.GetKey(KeyCode.UpArrow))
|
|
{
|
|
moveDirection = Vector3.forward * speed * Time.deltaTime;
|
|
transform.position += moveDirection;
|
|
}
|
|
if (Input.GetKey(KeyCode.DownArrow))
|
|
{
|
|
moveDirection = Vector3.back * speed * Time.deltaTime;
|
|
transform.position += moveDirection;
|
|
}
|
|
if (Input.GetKey(KeyCode.Space))
|
|
{
|
|
if (distanceToBall <= 0.1f)
|
|
{
|
|
if (rb)
|
|
{
|
|
rb.AddForce(transform.forward * kickStrength, ForceMode.Impulse);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (moveDirection != Vector3.zero) {
|
|
|
|
transform.rotation = Quaternion.LookRotation(moveDirection);
|
|
animator.SetBool("isRunning", true);
|
|
|
|
if (hasTheBall) {
|
|
Vector3 ballPosition = transform.position + (transform.forward.normalized * 1.0f) * Time.deltaTime;
|
|
ball.transform.position = ballPosition;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
animator.SetBool("isRunning", false);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|