Answer the question
In order to leave comments, you need to log in
The position of the entire prefab is changing, I didn’t change it, sort of. How to fix it?
(I clicked on the zombie)
I did not immediately understand that the position was changing, I thought that it just disappeared, but it was close to the sprite map. I absolutely do not understand why this happens, I did not enter a change in the position of these objects anywhere.
The code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickFeature : MonoBehaviour
{
public GameObject hero;
SpriteRenderer spriteRenderer;
private void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (GetComponentInChildren<HPCalc>() != null)
{
HPCalc = GetComponentInChildren<HPCalc>();
}
}
HPCalc HPCalc;
private void OnMouseDown()
{
if (HPCalc != null)
{
HPCalc.EnemyCalc();
}
else
{
hero.transform.position = this.transform.position;
}
transform.localScale = new Vector2(0.50f, 0.60f);
spriteRenderer.color = new Color(0.93f, 0.93f, 0.93f);
}
private void OnMouseUp()
{
transform.localScale = new Vector2(0.46f, 0.56f);
spriteRenderer.color = new Color(1, 1, 1);
}
private void OnMouseOver()
{
spriteRenderer.color = new Color(0.97f, 0.97f, 0.97f);
}
private void OnMouseExit()
{
spriteRenderer.color = new Color(1, 1, 1);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPCalc : MonoBehaviour
{
[SerializeField] public TextMesh hpTextMesh;
[SerializeField] public TextMesh dmgTextMesh;
int EnemyHP;
int EnemyDMG;
private void Start()
{
EnemyHP = int.Parse(hpTextMesh.text);
EnemyDMG = int.Parse(dmgTextMesh.text);
}
public HPnDMGHero HPnDMGHero;
public void EnemyCalc()
{
HPnDMGHero.HeroCalc(ref EnemyHP, ref EnemyDMG);
hpTextMesh.text = EnemyHP.ToString();
dmgTextMesh.text = EnemyDMG.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPnDMGHero : MonoBehaviour
{
[SerializeField] public TextMesh hpTextMesh;
[SerializeField] public TextMesh dmgTextMesh;
int HeroHP;
int HeroDMG;
private void Start()
{
HeroHP = int.Parse(hpTextMesh.text);
HeroDMG = int.Parse(dmgTextMesh.text);
}
public void HeroCalc(ref int HPEnemy, ref int DMGEnemy)
{
if (HeroHP > DMGEnemy)
{
HeroHP -= DMGEnemy;
}
if (HPEnemy > HeroDMG)
{
HPEnemy -= HeroDMG;
}
hpTextMesh.text = HeroHP.ToString();
dmgTextMesh.text = HeroDMG.ToString();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question