Hey.
so im trying to make a superweapon that builds up its damage power when the button is pressed and held, and is released when the player releases the button.
Ive managed to figure out the input system scripts, but the actual coding to make this happend confuses me, and i need your help.
So what i want to do is instantiate a prefab when the button is pressed, and that prefab keeps in place until the button is released. then it flies forward with a copy of the value the player accumulated.
the value should be clamped, between 0-50. but if the player releases the superweapon before it gets to the highest value. the weapon fires away with what value it got.
Im struggling to figure out how to get the instantiated copy to get whatever value it was given, grab a copy of the value, and not care about what ever the actual value is later on when its reduced back to zero.
is it best to have the value on the player for the superweapon to copy it from, or should the value be accumulated on the actual superweapon prefab?
and how to i access the prefabs values when its just a clone? that specific clones prefab.
if you dont have the time to explain it all, please refer to a page i can read up on.
thanx =)
Your input script needs a reference to whatever script is supposed to hold the value on the weapon. So the workflow would look something like this:
Press button, instantiate object, get reference to above mentioned script S
While button is pressed, increase local variable (or do it directly in S)
Once button is released, set S.value = localValue and set localValue = 0. And call S.launch() or whatever to make the weapon fire / the projectile fly off.
As you mentioned yourself, you do not actually need a localValue. This depends on whether or not the player / input handler needs this value for something else. If not, you can do the accumulation on the weapon itself.
As for how to get the actual reference, when you call Instantiate(prefab), that returns you a reference to the created gameobject: Unity - Scripting API: Object.Instantiate
You can now use this reference to get a reference to the attached script S you require to save the value on. So newObj.GetComponent() basically. The script S should hence have a public variable or public property allowing you to set the value.
thanx. i will give it a try. the thing is that confuses me is that how does the main object that instantiate the superweapon know that the instantiated superweapon is the one it should add the value to? what if the player happend to instantiate another superweapon (fairly impossible), would the accumulated value be given to both? like the main object just finds whatever has the superweapon script on itself and add a value.
is it because the one line of code that creates the superweapon also has the command to add the value aswell? so its doing a double function? spawning and slapping on a value and send it packing?
and the Superweaponscript on the actual spawned prefab.
using System.Collections;
using UnityEngine;
public class SuperWeaponScript : MonoBehaviour
{
public float damageValue;
private Rigidbody rb;
public float speed = 0;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
rb.velocity = transform.forward * speed;
}
}