205 lines
5.6 KiB
C#
205 lines
5.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using UnityEngine;
|
|
|
|
public class GerakanRaket : MonoBehaviour
|
|
{
|
|
// Variabel untuk Gyro
|
|
SerialPort stream = new SerialPort("COM6", 9600);
|
|
public string strReceived;
|
|
public string[] strData = new string[4];
|
|
public string[] strData_received = new string[4];
|
|
public float qw, qx, qy, qz;
|
|
|
|
// Variabel untuk Gerakan Raket
|
|
public Transform aimTarget;
|
|
public float horizontalSpeed = 0.5f;
|
|
public float verticalSpeed = 0.5f;
|
|
public float aimSpeed = 1.0f;
|
|
public AudioClip hitSoundClip;
|
|
|
|
private float initialHeight;
|
|
private Vector3 initialPosition;
|
|
|
|
private bool ballMoving = false;
|
|
|
|
void Start()
|
|
{
|
|
InitializeSerialPort();
|
|
initialHeight = transform.position.y;
|
|
initialPosition = transform.position;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.R))
|
|
{
|
|
ResetPlayerPosition();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (stream.IsOpen && stream.BytesToRead > 0)
|
|
{
|
|
strReceived = stream.ReadLine();
|
|
strData = strReceived.Split(',');
|
|
if (strData.Length == 4)
|
|
{
|
|
ProcessGyroData();
|
|
if (ballMoving) Move();
|
|
}
|
|
}
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError("Error reading from serial port: " + ex.Message);
|
|
ReconnectSerialPort();
|
|
}
|
|
}
|
|
|
|
void ProcessGyroData()
|
|
{
|
|
strData_received[0] = strData[0];
|
|
strData_received[1] = strData[1];
|
|
strData_received[2] = strData[2];
|
|
strData_received[3] = strData[3];
|
|
|
|
qw = float.Parse(strData_received[0]);
|
|
qx = float.Parse(strData_received[1]);
|
|
qy = float.Parse(strData_received[2]);
|
|
qz = float.Parse(strData_received[3]);
|
|
|
|
transform.rotation = new Quaternion(-qy, -qz, qx, qw);
|
|
}
|
|
|
|
void Move()
|
|
{
|
|
MoveHorizontal();
|
|
MoveVertical();
|
|
MoveAimTarget();
|
|
}
|
|
|
|
void MoveHorizontal()
|
|
{
|
|
float horizontalMovement = -qx * horizontalSpeed * Time.deltaTime;
|
|
Vector3 newPosition = transform.position;
|
|
newPosition.x += horizontalMovement;
|
|
newPosition.x = Mathf.Clamp(newPosition.x, -3f, 4.6f);
|
|
transform.position = newPosition;
|
|
}
|
|
|
|
void MoveVertical()
|
|
{
|
|
float verticalMovement = -qy * verticalSpeed * Time.deltaTime;
|
|
Vector3 newPosition = transform.position;
|
|
newPosition.z += verticalMovement;
|
|
newPosition.z = Mathf.Clamp(newPosition.z, -6.5f, 1.6f);
|
|
transform.position = newPosition;
|
|
}
|
|
|
|
void MoveAimTarget()
|
|
{
|
|
float aimHorizontalMovement = qz * aimSpeed * Time.deltaTime;
|
|
Vector3 aimTargetPosition = aimTarget.position;
|
|
aimTargetPosition.x += aimHorizontalMovement;
|
|
aimTarget.position = aimTargetPosition;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.CompareTag("Bola"))
|
|
{
|
|
HandleBallCollision(other);
|
|
}
|
|
}
|
|
|
|
void HandleBallCollision(Collider ballCollider)
|
|
{
|
|
ballMoving = true;
|
|
Vector3 dir = aimTarget.position - ballCollider.transform.position;
|
|
Rigidbody bolaRigidbody = ballCollider.GetComponent<Rigidbody>();
|
|
|
|
if (!bolaRigidbody.useGravity)
|
|
{
|
|
bolaRigidbody.useGravity = true;
|
|
}
|
|
|
|
float horizontalMovementDirection = -qz;
|
|
if (horizontalMovementDirection > 10)
|
|
{
|
|
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(1, 5, 1);
|
|
}
|
|
else if (horizontalMovementDirection < -10)
|
|
{
|
|
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(-1, 5, 1);
|
|
}
|
|
else
|
|
{
|
|
float verticalMovementDirection = -qy;
|
|
if (verticalMovementDirection > 0)
|
|
{
|
|
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(0, 5, 2);
|
|
}
|
|
else if (verticalMovementDirection < 0)
|
|
{
|
|
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(0, 7, 0);
|
|
}
|
|
else
|
|
{
|
|
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(0, 6, 0);
|
|
}
|
|
}
|
|
|
|
if (hitSoundClip != null)
|
|
{
|
|
AudioSource.PlayClipAtPoint(hitSoundClip, transform.position);
|
|
}
|
|
}
|
|
|
|
public void ResetPlayerPosition()
|
|
{
|
|
Debug.Log("Resetting Player Position");
|
|
transform.position = initialPosition;
|
|
ballMoving = false;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (stream != null && stream.IsOpen)
|
|
{
|
|
stream.Close();
|
|
}
|
|
}
|
|
|
|
void InitializeSerialPort()
|
|
{
|
|
try
|
|
{
|
|
stream.Open();
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError("Error opening serial port: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
void ReconnectSerialPort()
|
|
{
|
|
try
|
|
{
|
|
if (stream != null && stream.IsOpen)
|
|
{
|
|
stream.Close();
|
|
}
|
|
stream.Open();
|
|
Debug.Log("Serial port reconnected successfully.");
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError("Error reconnecting serial port: " + ex.Message);
|
|
}
|
|
}
|
|
}
|