英文:
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 && 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 <= 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 && 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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论