Skip to content

Instantly share code, notes, and snippets.

@satomi-i-k
Last active October 3, 2023 09:33
Show Gist options
  • Save satomi-i-k/4bf80541f39fe561288f244cad8eed52 to your computer and use it in GitHub Desktop.
Save satomi-i-k/4bf80541f39fe561288f244cad8eed52 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Raq_opponent : MonoBehaviour
{
public float speed;
public float turnSpeed;
public Animator anim;
public Transform head;
public Transform lookAt;
public Transform rubberball;
public float force;
public float upForce;
public float CeilingForce_y;
public float CeilingForce_z;
public Transform player;
public Transform servePoint;
public float moveRange_x;
public float moveRange_y;
public float frontwall_pos = 12.2f;
float rotation;
float lastDist;
public bool notRotating;
Vector3 target;
Vector3 pos;
Vector3 move_target;
Vector3 ballTarget;
bool right;
bool move_action;
bool move;
bool justHit;
Rigidbody rb;
RubberBall ballScript;
public Raq_GameManager raq_GameManager;
Raq_GameManager gamemanagerScript;
void Start()
{
rb = rubberball.GetComponent<Rigidbody>();
ballScript = rubberball.GetComponent<RubberBall>();
gamemanagerScript = raq_GameManager.GetComponent<Raq_GameManager>();
}
 void Update()
 {
// set target as ball position
target = rubberball.position;
if (!gamemanagerScript.befServe && ballScript.GetLastHit() && !ballScript.after_hit)
  {
//check if character should move
Raq_Move();
}
}
void LateUpdate()
  {
//look at the opponent
head.LookAt(lookAt.position);
//keep the serve position in the same place after moving bones
pos = transform.position;
}
void Raq_Move()
  {
float target_dist = Vector3.Distance(transform.position, target);
move_action = target_dist > 0.1f;
if(move_action)
{
pos = transform.position;
move_target = new Vector3(target.x - 0.02f, -2.5f, target.z);
move = rubberball.GetComponent<RubberBall>().GetLastHit();
ballTarget = move ? move_target : pos;
transform.position = Vector3.MoveTowards(transform.position, ballTarget, Time.deltaTime * speed * 0.6f);
right = target.x > transform.position.x;
}
if(anim.GetBool("Right") != right)
anim.SetBool("Right", right);
if(!move_action)
   {
rotation = 0;
}
else
    {
if(right)
   {
rotation = -90;
}
else
   {
rotation = 91;
}
}
//update the animator value to display the correct animation
anim.SetBool("Moving", move_action);
//rotate character based on movement
Vector3 rot = transform.eulerAngles;
rot.y = rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rot), Time.deltaTime * turnSpeed);
}
void OnTriggerEnter(Collider other)
  {
//shoot the ball on trigger enter (when ball enters the opponent box)
if(!other.gameObject.CompareTag("Ball") || other.gameObject.GetComponent<RubberBall>().inactive || justHit || gamemanagerScript.befServe || !ballScript.GetLastHit())
return;
float xDistance = Mathf.Abs(transform.position.x - other.gameObject.transform.position.x);
if(xDistance > 1.3f)
  {
if(Random.Range(0, 2) == 0)
anim.SetTrigger("Hit right");
return;
}
StartCoroutine(Raq_JustHit());
anim.SetTrigger("Hit right");
Raq_HitBall(false, null);
}
//shoot the ball in a random direction and update the ball and the player
public void Raq_HitBall(bool noFlame, Transform spawnPosition)
 {
Vector3 random = new Vector3(Random.Range(-moveRange_x, moveRange_x), Random.Range(-moveRange_y, moveRange_y) + rubberball.position.y, frontwall_pos);
Vector3 direction = (random - transform.position).normalized;
player.GetComponent<Player>().SetTarget(random);
direction = new Vector3(direction.x * force, direction.z * force * CeilingForce_y, direction.z * force * CeilingForce_z);
EventManager.Instance.TriggerRaqplayerHit(direction);
ballScript.SetLastHit(false);
}
//make sure we don't hit twice at the same time
public IEnumerator Raq_JustHit()
  {
justHit = true;
yield return new WaitForSeconds(1f);
justHit = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment