I’ve created Dino object, which is the enemy within the sport and it must be on the a number of positions within the sport (3 or 4). So I’ve created a script for it and I would like one thing like that:
When participant is near it, it ought to change it is velocity, path perhaps, and the animation.
I’ve a script which is hooked up to the Dino Object. After I make the copies of the Dino object within the undertaking.
The issue is, when participant is near the primary Dino the modifications are made on all of the Dinos within the Scene. So how can I repair that? Right here is my Dino Script:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class Dino : MonoBehaviour
{
non-public float velocity = 1f;
non-public Vector2 tempPos;
non-public SpriteRenderer sr;
non-public float playerPos;
non-public float dinoPos;
non-public Animator animator;
void Awake()
{
sr = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
// Begin is known as earlier than the primary body replace
void Begin()
{
}
// Replace is known as as soon as per body
void Replace()
{
DinoMovement();
}
non-public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
sr.flipX = !sr.flipX;
velocity = -speed;
}
}
void DinoMovement()
{
playerPos = GameObject.Discover("Participant").remodel.place.x;
//wallPos = GameObject.FindGameObjectWithTag("Wall").remodel.place.x;
dinoPos = remodel.place.x;
//transfer Dino
tempPos = remodel.place;
tempPos.x += velocity * Time.deltaTime;
remodel.place = tempPos;
//Dino Follows Participant
int pictw = WallDestroy.PlayerIsCloseToWall;
if (pictw == 1)
{
if (playerPos < dinoPos - 2)
{
velocity = -2f;
sr.flipX = true;
animator.SetBool("DinoRunParam", true);
}
else if (playerPos > dinoPos + 2)
{
velocity = 2f;
sr.flipX = false;
animator.SetBool("DinoRunParam", true);
}
}
}
}
Right here is the clear wall script, which controls if the Participant is near that wall, when Dino ought to change some properties.
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class WallDestroy : MonoBehaviour
{
non-public float tempPos;
non-public float playerPos;
non-public static int playerIsCloseToWall = 0;
// Begin is known as earlier than the primary body replace
void Begin()
{
}
// Replace is known as as soon as per body
void Replace()
{
tempPos = remodel.place.x;
playerPos = GameObject.Discover("Participant").remodel.place.x;
if(tempPos - playerPos < 7)
{
playerIsCloseToWall = 1;
Destroy(this.gameObject);
}
}
public static int PlayerIsCloseToWall
{
get { return playerIsCloseToWall; }
set { playerIsCloseToWall = worth; }
}
}
Right here is the Participant Script if wanted:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class MaleZombie : MonoBehaviour
{
[SerializeField]
non-public float velocity = 3f;
non-public float jumpForce = 6f;
non-public bool isGrounded = true;
non-public Vector2 tempPos;
non-public SpriteRenderer sr;
non-public Animator animator;
non-public Rigidbody2D rb;
// Awake
void Awake()
{
sr = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
// Begin is known as earlier than the primary body replace
void Begin()
{
}
// Replace is known as as soon as per body
void Replace()
{
ZombieMove();
ZombieJump();
ZombieAttack();
}
// This operate is known as as soon as per 0.02 sec.
void FixedUpdate()
{
}
void ZombieMove()
{
tempPos = remodel.place;
float h = Enter.GetAxisRaw("Horizontal");
if (h > 0)
{
tempPos.x += velocity * Time.deltaTime;
sr.flipX = false;
animator.SetBool("WalkParam", true);
}
else if (h < 0)
{
tempPos.x -= velocity * Time.deltaTime;
sr.flipX = true;
animator.SetBool("WalkParam", true);
}
else
{
animator.SetBool("WalkParam", false);
}
remodel.place = tempPos;
}
void ZombieJump()
{
if (Enter.GetKeyDown(KeyCode.UpArrow) && isGrounded == true)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
isGrounded = false;
}
}
non-public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isGrounded = true;
}
}
void ZombieAttack()
{
if (Enter.GetKeyDown(KeyCode.House))
// if (Enter.GetButtonDown("Bounce"))
{
animator.Play("Assault");
}
}
}
```