A
A
Alexander Pianov2019-07-11 00:59:50
C++ / C#
Alexander Pianov, 2019-07-11 00:59:50

Why does Instantiate make each new object appear higher than the previous one?

In the ability window, the player selects the desired one and clicks on the ground. An object appears there, for example, a fire support turret. In X and Z, the turret corresponds to the hitpoint of the raycast. However, its Y position is higher. And with subsequent creations of turrets, their Y positions are doubled. As a result, the new turrets end up in outer space and cannot provide fire support.
Tell me, what is the reason, brothers, I am attaching the code:

using UnityEngine;

public class StrikeMenu : MonoBehaviour
{

    private bool startStrike = false;
    
    private int IndexStrike = -1;           
    private int IndexStrikeMenuOpen = -1;   
    
    public StrikeMode SMode = null;        
    
    private Ray _ray;
    private RaycastHit _hit;
    private RaycastHit pointSpawn;
            
    public GameObject _openstrikemenu;      
    public GameObject _openbutton;
    public GameObject _closebutton;
    public GameObject _cancelbutton;



    private void Start()
    {

        _openstrikemenu.gameObject.SetActive(false);
        _closebutton.gameObject.SetActive(false);
        _cancelbutton.gameObject.SetActive(false);
        
    }


    private void Update()
    {

        if (startStrike == true)

        {

            if (Input.GetMouseButton(button: 0))

            {
                
                if (SMode.WarTimer >= SMode.StrikeList[IndexStrike].Reload)
                    
                {

                    _ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(_ray, out _hit))

                    {
                                                
                            Instantiate (SMode.StrikeList[IndexStrike].Strike, new Vector3(_hit.point.x, _hit.point.y, _hit.point.z), Quaternion.identity);
                                                    
                            SMode.WarTimer -= SMode.StrikeList[IndexStrike].Reload;
                            
                            _openbutton.gameObject.SetActive(true);
                            _cancelbutton.gameObject.SetActive(false);

                            IndexStrike = -1;

                            startStrike = false;
                            
                    }
                }
            }
        }
     }

    public void StartStrike (int ButtonClickStrike)                        
    {
        if (SMode.WarTimer >= SMode.StrikeList[ButtonClickStrike].Reload)
        {
            
            startStrike = true;

            IndexStrike = ButtonClickStrike; 
                                    
            _openstrikemenu.gameObject.SetActive(false);
            _closebutton.gameObject.SetActive(false);
            _openbutton.gameObject.SetActive(false);
            _cancelbutton.gameObject.SetActive(true);

            IndexStrikeMenuOpen = -1;
            Debug.Log("Start Strike");
        }
    }

// далее часть про UI

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question