MIF_E31211305/botHard.cs

57 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BotHard : MonoBehaviour
{
float speed = 0.9f;
public Transform Bola;
public Transform hitTarget;
public AudioClip hitSoundClip;
float force = 6;
float jarakZDariBot = 7.5f;
float zMin = 7.2f;
float zMax = 9f;
Vector3 targetPosition;
void Start()
{
targetPosition = transform.position;
}
void Update()
{
Move();
hitTarget.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - jarakZDariBot);
}
void Move()
{
targetPosition.x = Bola.position.x;
float targetZ = Mathf.Clamp(Bola.position.z, zMin, zMax);
targetPosition.z = targetZ;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bola"))
{
Vector3 dir = hitTarget.position - other.transform.position;
Rigidbody bolaRigidbody = other.GetComponent<Rigidbody>();
bolaRigidbody.useGravity = true;
bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0);
if (hitSoundClip != null)
{
AudioSource.PlayClipAtPoint(hitSoundClip, transform.position);
}
}
}
}