unity3d - Programming a 2d enemy in c# -
i have been working on enemies in game, feel code clunky , not seem work. put code player script , enemy script. player script follows:
edit: sorry not being clear enough, here lines of code not work , want them do:
void ontriggerenter2d (collider2d other) { if (other.gameobject.tag == "enemy") { destroy (this.gameobject); } }
what want in code when player encounters enemy or object "enemy" tag, touching object destroy or kill player object.
void ontriggerenter2d (collider2d other) { if (other.gameobject.tag == "killable") { debug.log ("entering killzone!"); destroy (other.gameobject); instantiate (deadstar, transform.position, quaternion.identity); this.gameobject.setactive (false); } } }
this code loosely copied previous game, has bits of it's killzone , made destroy player object when touches object contains script. (the player tagged "killable")
hope edit helps, im little tired , forgot specify things, sorry that.
using unityengine; using system.collections; public class playercontroller : monobehaviour { public float jumpspeed; public float scale; public string jumpkey; public string leftkey; public string rightkey; public float speed; public gameobject player; public rigidbody2d rb; void ontriggerenter2d (collider2d other) { if (other.gameobject.tag == "enemy") { destroy (this.gameobject); } } void start() { getcomponent<rigidbody2d>().freezerotation = true; /*getcomponent<rigidbody>().angularvelocity = vector3.zero;*/ rb = getcomponent (typeof(rigidbody2d)) rigidbody2d; } void fixedupdate () { vector3 hold = rb.velocity; hold.x = 0; if (input.getkey (leftkey)) { hold.x -= speed; } if (input.getkey (rightkey)) { hold.x += speed; } if (input.getkeydown (keycode.space)) { hold.y = jumpspeed; } rb.velocity = hold; }
}
the enemy script follows:
using unityengine; using system.collections; public class enemykillzone : monobehaviour { public gameobject deadstar; public gameobject player; void ontriggerenter2d (collider2d other) { if (other.gameobject.tag == "killable") { debug.log ("entering killzone!"); destroy (other.gameobject); instantiate (deadstar, transform.position, quaternion.identity); this.gameobject.setactive (false); } } }
Comments
Post a Comment