[ad_1]
I’ve created Dino object, which is the enemy within the recreation and it must be on the a number of positions within the recreation (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 pace, path possibly, 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 downside 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? Here is my Dino Script:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class Dino : MonoBehaviour
{
non-public float pace = 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>();
}
// Start known as earlier than the primary body replace
void Start()
{
}
// Update known as as soon as per body
void Update()
{
DinoMotion();
}
non-public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.recreationObject.CompareTag("Wall"))
{
sr.flipX = !sr.flipX;
pace = -speed;
}
}
void DinoMotion()
{
playerPos = RecreationObject.Find("Player").rework.place.x;
//wallPos = RecreationObject.FindRecreationObjectWithTag("Wall").rework.place.x;
dinoPos = rework.place.x;
//transfer Dino
tempPos = rework.place;
tempPos.x += pace * Time.deltaTime;
rework.place = tempPos;
//Dino Follows Player
int pictw = WallDestroy.PlayerIsCloseToWall;
if (pictw == 1)
{
if (playerPos < dinoPos - 2)
{
pace = -2f;
sr.flipX = true;
animator.SetBool("DinoRunParam", true);
}
else if (playerPos > dinoPos + 2)
{
pace = 2f;
sr.flipX = false;
animator.SetBool("DinoRunParam", true);
}
}
}
}
Here is the clear wall script, which controls if the Player 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;
// Start known as earlier than the primary body replace
void Start()
{
}
// Update known as as soon as per body
void Update()
{
tempPos = rework.place.x;
playerPos = RecreationObject.Find("Player").rework.place.x;
if(tempPos - playerPos < 7)
{
playerIsCloseToWall = 1;
Destroy(this.recreationObject);
}
}
public static int PlayerIsCloseToWall
{
get { return playerIsCloseToWall; }
set { playerIsCloseToWall = worth; }
}
}
Here is the Player Script if wanted:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class MaleZombie : MonoBehaviour
{
[SerializeField]
non-public float pace = 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>();
}
// Start known as earlier than the primary body replace
void Start()
{
}
// Update known as as soon as per body
void Update()
{
ZombieMove();
ZombieJump();
ZombieAttack();
}
// This perform known as as soon as per 0.02 sec.
void MountedUpdate()
{
}
void ZombieMove()
{
tempPos = rework.place;
float h = Input.GetAxisRaw("Horizontal");
if (h > 0)
{
tempPos.x += pace * Time.deltaTime;
sr.flipX = false;
animator.SetBool("WalkParam", true);
}
else if (h < 0)
{
tempPos.x -= pace * Time.deltaTime;
sr.flipX = true;
animator.SetBool("WalkParam", true);
}
else
{
animator.SetBool("WalkParam", false);
}
rework.place = tempPos;
}
void ZombieJump()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && isGrounded == true)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
isGrounded = false;
}
}
non-public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.recreationObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void ZombieAttack()
{
if (Input.GetKeyDown(KeyCode.Space))
// if (Input.GetButtonDown("Jump"))
{
animator.Play("Attack");
}
}
}
```
[ad_2]