- Home /
Creating a Shield
Hi, I'm currently working on making a Hack 'n Slash, and I'm relatively new to Unity and C#, and I was hoping someone would be able to help me create a shield for my character (that could hopefully be used for the enemy as well).
I've been using tons of tutorials and books, but I can't seem to figure out how to create a shield for the player. In this case when the player presses or holds the right mouse button the player would block the enemies attacks. So if anyone could help me out on creating the shield or send me a tutorial on how to do it, I'll be grateful. Thanks in advance!
Answer by J3-Gaming · Sep 20, 2011 at 12:14 AM
This website is for scripting help, are you talking about creating the model asset of a shield?
Try the unity forum
Answer by matthew_tavares1 · Sep 20, 2011 at 01:06 AM
No I'm talking about creating the code for the player to block. When the player is holding down the button it'll protect him completely from all hits. He'll still be able to move but not attack.
Answer by zmar0519 · Sep 20, 2011 at 01:09 AM
well, if you could try this:
var reflectivePower : float;
private var active : boolean;
function Update() {
if(Input.GetMouseButtonDown(0)) {
active = true;
}
renderer.enabled = active;
}
function OnTriggerEnter(col : Collider) {
if(active) {
var dir : Vector3 = col.gameObject.rigidbody.velocity;
dir = -dir;
col.gameObject.rigidbody.velocity = dir * reflectivePower;
}
}
it will reflect all rigid bodies that hit your shield object, which needs to be a trigger.
Thanks for the help, however I've been using C# for this game, and I'm not at all familiar with Java. Is there anyway you could help me put this in C#?
Your answer