c# - I want an object(bird) rotate right when falling -
there code: watched tutorial on youtube (https://www.youtube.com/watch?v=dwtob3j1ytg). tried follow steps, misspelled because rotating right while falling thing not working me :( please help! if need more information ask me!
using unityengine; using system.collections; public class birdmovement : monobehaviour { vector3 velocity = vector3.zero; public vector3 gravity; public vector3 flapvelocity; public float maxspeed = 5f; public float forwardspeed = 1f; bool didflap = false; // graphic & input updates here void update() { if (input.getkeydown(keycode.space) || input.getmousebuttondown(0)) { didflap = true; } } // physics engine updates here void fixedupdate () { velocity.x = forwardspeed; velocity += gravity * time.deltatime; if (didflap == true) { didflap = false; if(velocity.y < 0) velocity.y = 0; velocity += flapvelocity; } velocity = vector3.clampmagnitude (velocity, maxspeed); transform.position += velocity * time.deltatime; float angle = 0; if(velocity.y < 0) { angle = mathf.lerp (0, -90, velocity.y / maxspeed); } transform.rotation = quaternion.euler (0,0,angle); } }
i followed tutorial code looks like. missing animator
public class birdmovement : monobehaviour { vector3 velocity = vector3.zero; public float flapspeed = 100f; public float forwardspeed = 1f; bool didflap = false; animator animator; public bool dead = false; float deathcooldown; public bool godmode = false; void start () { animator = transform.getcomponentinchildren<animator>(); if(animator == null) debug.logerror("failed find animator!"); } void update() { if(dead) { deathcooldown -= time.deltatime; // should , how comes in tutorial. if(deathcooldown <= 0) if(input.getkeydown(keycode.space) || input.getmousebuttondown(0)) application.loadlevel( application.loadedlevel ); } else if(input.getkeydown(keycode.space) || input.getmousebuttondown(0)) didflap = true; } // physics engine updates here void fixedupdate () { if(dead) return; rigidbody2d.addforce( vector2.right * forwardspeed ); if(didflap) { rigidbody2d.addforce( vector2.up * flapspeed ); animator.settrigger("doflap"); didflap = false; } if(rigidbody2d.velocity.y > 0) transform.rotation = quaternion.euler(0, 0, 0); else { float angle = mathf.lerp (0, -90, (-rigidbody2d.velocity.y / 3f) ); transform.rotation = quaternion.euler(0, 0, angle); } } void oncollisionenter2d(collision2d collision) { if(godmode) return; animator.settrigger("death"); dead = true; deathcooldown = 0.5f; } }
Comments
Post a Comment