将刚刚点击的对象传递给函数?Unity c#

huangapple go评论70阅读模式
英文:

How to give the just clicked object to a function? Unity c#

问题

在检测到刚刚点击的方块的脚本中,您需要确保 "Smite" 函数知道要伤害和销毁哪个方块。以下是相应部分的更改:

检测点击的方块的脚本:

private void Update()
{
    if (HeroAbilities.ReadyingSmite && Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            GameObject clickedObject = hit.collider.gameObject;
            Vector3 clickedPosition = hit.collider.transform.position;

            // 调用 Smite 函数并传递点击的方块作为参数
            HeroAbilities.Smite(clickedObject);
        }
    }
}

Smite 脚本:

public void Smite(GameObject clickedObject)
{
    int AbilityDamage = Range(7, 10);
    int Damage = crusaderscript.BaseDamage + AbilityDamage;
    
    // 伤害传递给传递的方块对象
    clickedObject.GetComponent<YourCubeScript>().Health -= Damage;

    if (clickedObject.GetComponent<YourCubeScript>().Health <= 0)
    {
        // 销毁传递的方块对象
        Destroy(clickedObject);
    }
}

请确保在调用 "Smite" 函数时传递了正确的方块对象,以便在其中伤害和销毁它。

英文:

I have a script, where assuming ReadyingSmite, a boolean from another function called HeroAbilities, is active after i click a cube, it gives me the information of said cube and activates a function, Smite, which damages the cube, destroying it if the damage is high enough. How do i make it so Smite knows which cube to damage and destroy?

Script for detecting what cubes was just clicked

private void Update()
    {
        if (HeroAbilities.ReadyingSmite &amp;&amp; Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
     
                GameObject clickedObject = hit.collider.gameObject;
                Vector3 clickedPosition = hit.collider.transform.position;

                
                HeroAbilities.Smite();
            }
        }
    }

And the Smite script

public void Smite()
    {
        int AbilityDamage = Range(7, 10);
        int Damage = crusaderscript.BaseDamage + AbilityDamage ;
        ClickedObject.Health -= Damage ;
        if(ClickedObject.Health &lt;= 0)
        {
            Destroy(ClickedObject);
        }
    }

I need the places where "ClickedObject" is to be a reference to the clicked cube

答案1

得分: 1

We pass in the object we just clicked into our Smite function which will destroy it if health >= 0

{
    if (HeroAbilities.ReadyingSmite && Input.GetMouseButtonDown(0))
    {
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
        if (Physics.Raycast(ray, out var hit))
        {
            // check if hit object has the expected component attached
            if(hit.collider.TryGetComponent<TheScriptThatRefrenceTheHealth>(out var clickedObject))
            {
                // We pass in our clicked Object that we want to destroy
                HeroAbilities.Smite(clickedObject); 
            }
        }
    }
}
// we make this function receive a TheScriptThatRefrenceTheHealth 
public void Smite(TheScriptThatRefrenceTheHealth clickedObject)
{
    var AbilityDamage = Range(7, 10);
    var Damage = crusaderscript.BaseDamage + AbilityDamage ;
    clickedObject.Health -= Damage ;
    if(clickedObject.Health <= 0)
    {
        Destroy(clickedObject.gameObject);
    }
}
英文:

We pass in the object we just clicked into our Smite function which will destroy it if health >= 0

private void Update()
{
    if (HeroAbilities.ReadyingSmite &amp;&amp; Input.GetMouseButtonDown(0))
    {
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
        if (Physics.Raycast(ray, out var hit))
        {
            // check if hit object has the expected component attached
            if(hit.collider.TryGetComponent&lt;TheScriptThatRefrenceTheHealth&gt;(out var clickedObject))
            {
                // We pass in our clicked Object that we want to destroy
                HeroAbilities.Smite(clickedObject); 
            }
        }
    }
}
// we make this function receive a TheScriptThatRefrenceTheHealth 
public void Smite(TheScriptThatRefrenceTheHealth clickedObject)
{
    var AbilityDamage = Range(7, 10);
    var Damage = crusaderscript.BaseDamage + AbilityDamage ;
    clickedObject.Health -= Damage ;
    if(clickedObject.Health  &lt;= 0)
    {
        Destroy(clickedObject.gameObject);
    }
}

huangapple
  • 本文由 发表于 2023年5月24日 20:43:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定