Hi! I am new to the Unity scene as well as Javascripting and was messing around, making a 2D shooter for fun and was wondering how to implement a permanent shield status on the player. I viewed the tutorials and some videos and came up with something close to in Java. Under my shield script I have something like:
function Update () {
// amount to move shield
amtToMove = shieldSpeed * Time.deltaTime;
if(transform.position.y >= -3){
Destroy(gameObject);
}
}
And under my player script, I have something like:
if(Input.GetKeyUp(“space”)){
var tempShield: Rigidbody;
tempShield = Instantiate(shield, transform.position, transform.rotation);
}
I made my shield sort of like a bullet that doesn’t move and can only be activated by hitting space. I sort of need help coming up with a solution for making, as stated before, a permanent shield and possibly being able to turn it off with a press of a button (though I could probably figure that out once I know how to set up that permanent shield status). Any help would be appreciated, thanks!
Create a sphere primitive, apply an appropriate shader to it. The shield shader from the wiki for example.
http://www.unifycommunity.com/wiki/index.php?title=Shield
Give it a sphere collider and make it a trigger. Destroy anything that’s a projectile that comes into the trigger area. Parent the assembly by dragging it onto your spaceship and resetting position. Enable/Disable the contraption when appropriate.
Fyi, cyan on white is a terrible choice for including code in a post.
Please use the code tag.
Oh sorry about the coloring. Already placed this under my shield script:
function OnTriggerEnter(otherObject: Collider ){
if(otherObject.gameObject.tag == "enemy"){
otherObject.gameObject.transform.position.y = 9;
}
But I do not know how to enable/disable the contraption permanently still? I did all the parenting, etc. I also tried editing my playerscript with
if(Input.GetKey("space")){
but with this, I still have to hold down the key in order to initiate the shield. How would one write out the code to just enable/disable the shield? That’s the part I am having difficulties with. Any help would be appreciated and thanks for the quick response!
Switch to using GetKeyDown if you want a toggle.