Hello, first time posting here !
I’m doing a final project of game dev in Unity 3d for my game dev course, (cs student)
and I’ve borrowed this script from a friend, in which I wanted to do a similar thing that he did.
I have this asset for the monsters: https://assetstore.unity.com/packages/3d/characters/creatures/rpg-monster-duo-pbr-polyart-157762
And my requirements for the script are:
- make the monsters go around a designated monster room (the room exists).
- make them take damage from when my weapons (sword, bow) touch it and eventually die with a proper animation
- do a quest when entering the monster room, a message will pop and then count the number of monsters killed till 10 (and the reward is a key)
I tried playing with the borders on this script but for some reason, it seems like it doesn’t really change their behavior and I’m not sure why
there is also animation transfer in the script so I want that to stay.
I’ve tried messing with chatGPT as well, but not sure what is wrong here so any help is highly appreciated !
thx again
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;
public class TurtleBehaviour : MonoBehaviour
{
Animator animator; // Drag the Animator component here in the Inspector
public float moveSpeed = 7f; // Speed of the slime movement
public float minPauseTime = 1f; // Minimum time to pause when reaching a destination
public float maxPauseTime = 3f; // Maximum time to pause when reaching a destination
public int maxX; // Maximum X
public int minX; // Minimum X
public int maxZ; // Maximum Z
public int minZ; // Minimum Z
public float y; // Y height
private Vector3 targetPosition; // The current target position the slime is moving towards
private bool isMoving = false; // To track if the slime is currently moving
void Start()
{
// Set the initial target position
animator = GetComponent<Animator>();
SetNewTargetPosition();
}
void Update()
{
// Check if the slime is close enough to the target position
if (isMoving)
{
float distance = Vector3.Distance(transform.position, targetPosition);
if (distance < 2.5f) // Close enough to the target position
{
isMoving = false;
animator.SetInteger("State", 0);// Switch to IdleBattle animation
StartCoroutine(PauseBeforeMovingAgain());
}
else
{
MoveTowardsTarget();
}
}
}
void MoveTowardsTarget()
{
// Move towards the target position
Vector3 direction = (targetPosition - transform.position).normalized;
transform.position += direction * moveSpeed * Time.deltaTime;
// Rotate towards the target direction
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * moveSpeed);
// Set the RunFWD animation
animator.SetInteger("State", 1);
}
void SetNewTargetPosition()
{
// Generate a random position within the room bounds
float x = Random.Range(minX, maxX);
float z = Random.Range(minZ, maxZ);
targetPosition = new Vector3(x, y, z);
float distance = Vector3.Distance(transform.position, targetPosition);
isMoving = true;
}
IEnumerator PauseBeforeMovingAgain()
{
// Pause for a random time before moving to the next position
float pauseTime = Random.Range(minPauseTime, maxPauseTime);
yield return new WaitForSeconds(pauseTime);
SetNewTargetPosition(); // Set a new random target position
}
}