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

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

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

问题

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

检测点击的方块的脚本:

  1. private void Update()
  2. {
  3. if (HeroAbilities.ReadyingSmite && Input.GetMouseButtonDown(0))
  4. {
  5. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  6. RaycastHit hit;
  7. if (Physics.Raycast(ray, out hit))
  8. {
  9. GameObject clickedObject = hit.collider.gameObject;
  10. Vector3 clickedPosition = hit.collider.transform.position;
  11. // 调用 Smite 函数并传递点击的方块作为参数
  12. HeroAbilities.Smite(clickedObject);
  13. }
  14. }
  15. }

Smite 脚本:

  1. public void Smite(GameObject clickedObject)
  2. {
  3. int AbilityDamage = Range(7, 10);
  4. int Damage = crusaderscript.BaseDamage + AbilityDamage;
  5. // 伤害传递给传递的方块对象
  6. clickedObject.GetComponent<YourCubeScript>().Health -= Damage;
  7. if (clickedObject.GetComponent<YourCubeScript>().Health <= 0)
  8. {
  9. // 销毁传递的方块对象
  10. Destroy(clickedObject);
  11. }
  12. }

请确保在调用 "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

  1. private void Update()
  2. {
  3. if (HeroAbilities.ReadyingSmite &amp;&amp; Input.GetMouseButtonDown(0))
  4. {
  5. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  6. RaycastHit hit;
  7. if (Physics.Raycast(ray, out hit))
  8. {
  9. GameObject clickedObject = hit.collider.gameObject;
  10. Vector3 clickedPosition = hit.collider.transform.position;
  11. HeroAbilities.Smite();
  12. }
  13. }
  14. }

And the Smite script

  1. public void Smite()
  2. {
  3. int AbilityDamage = Range(7, 10);
  4. int Damage = crusaderscript.BaseDamage + AbilityDamage ;
  5. ClickedObject.Health -= Damage ;
  6. if(ClickedObject.Health &lt;= 0)
  7. {
  8. Destroy(ClickedObject);
  9. }
  10. }

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

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

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

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

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:

确定