H
H
Hixac2020-12-31 18:13:20
Unity
Hixac, 2020-12-31 18:13:20

The position of the entire prefab is changing, I didn’t change it, sort of. How to fix it?

5fede879185df307533330.png

(I clicked on the zombie)
5fede8916b3d2879775520.png

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

spoiler

при клике по карте
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);
    }
}


функция HPCalc
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();
    }
}


и ещё одна функция HeroCalc, которую может быть не стоило делать?
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

1 answer(s)
H
Hixac, 2020-01-03
@Hixac

I found the answer, unity discord helped me, the official server.
In general, more to the point, Vector2(1, 1), the third value is set to zero by default.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question