As required, you cannot use the Charactorcontroll component because you want to simulate the effect of a zombie being hit, you can only use the Rigidbody component.
First put the zombie and camera position in the scene, here will not add script to the camera, directly fixed.
And then added a capsule-type collision box for the zombie to detect the mouse click, of course, the rigid body components are not few.
Then is the control of the zombie script, due to the needs of the topic, the comments are all written in English, but should not be ugly to understand.
Start by setting different values and initializing:
Public floatMove_speed=10f; Public floatforce_x=0f,force_y=200f,force_z=200f; Transform M_transform,m_camera,blood; Animation m_animation; Rigidbody M_rigidbody; string[] clipname={"Idle","Crawl","shamble","Run","FallBack","hit1","Hit2"}; BOOLis_hit=false, is_die=false; Public inthp=1;//Zombie ' s hp,when 0 Zombie die//Use this for initialization voidStart () {M_camera= Gameobject.findgameobjectwithtag ("Maincamera"). Transform; M_transform= This. Transform; M_animation= M_transform. Getcomponent<animation> (); M_rigidbody=m_transform. Getcomponent<rigidbody> (); Blood=resources.load ("Partice/blood_out",typeof(Transform)) asTransform; }
Then you have to give the zombie a different animation according to the movement speed of the zombie, in theory it will be better to use the animation state machine, here is the simple switch playback:
if (Move_speed < 0.001f ) m_animation. Play (clipname [ 0 ]); else if (move_speed < 1f) m_animation. Play (clipname [ 1 ]); else if (move_speed < 3f) m_animation. Play (clipname [ 2 ]); else M_animation. Play (clipname [ 3 ]);
Then let the zombie face the camera and move forward:
M_transform. LookAt (M_camera); M_rigidbody.transform.position+ = (m_camera.position-m_transform.position). normalized*time.deltatime*move_ Speed // Zombie LookAt camera and move to it
The last is to click on the zombie to give the zombie damage, and simulate a hit fly:
void OnMouseDown () { if (! Is_die) { m_rigidbody. Addforce (new Vector3 (force_x, force_y, force_z)); // Add a force if zombie not dead Startcoroutine (Blood_out ()); // play the blood animator hp--; if (0 = = hp) Startcoroutine (Die ()) ; Else Startcoroutine (Hit ()); } }
The above blood_out is a very primitive animation I made with the particle system, and I won't let it out.
There are also some co-processes for controlling animations:
IEnumerator die () {Is_die=true; yield returnStartcoroutine (Playanimation (clipname[4],1f)); yield return NewWaitforseconds (3f);//After 3 seconds,destory the dead zombieDestroy (M_transform.gameobject); } IEnumerator Hit () {Is_hit=true; yield returnStartcoroutine (Playanimation (clipname[6],0.2f)); Is_hit=false; } IEnumerator playanimation (stringNamefloatTime ) {m_animation. Play (name); yield return Newwaitforseconds (time); }
The entire full script:
usingUnityengine;usingSystem.Collections; Public classZombiecontroll:monobehaviour { Public floatMove_speed=10f; Public floatforce_x=0f,force_y=200f,force_z=200f; Transform M_transform,m_camera,blood; Animation m_animation; Rigidbody M_rigidbody; string[] clipname={"Idle","Crawl","shamble","Run","FallBack","hit1","Hit2"}; BOOLis_hit=false, is_die=false; Public inthp=1;//Zombie ' s hp,when 0 Zombie die//Use this for initialization voidStart () {M_camera= Gameobject.findgameobjectwithtag ("Maincamera"). Transform; M_transform= This. Transform; M_animation= M_transform. Getcomponent<animation> (); M_rigidbody=m_transform. Getcomponent<rigidbody> (); Blood=resources.load ("Partice/blood_out",typeof(Transform)) asTransform; } //Update is called once per frame voidUpdate () {if(HP >0&&!is_hit) { if(Move_speed <0.001f) m_animation. Play (Clipname [0]); Else if(Move_speed <1f) m_animation. Play (Clipname [1]); Else if(Move_speed <3f) m_animation. Play (Clipname [2]); Elsem_animation. Play (Clipname [3]);//The animator depend on the Move_speedM_transform. LookAt (M_camera); M_rigidbody.transform.position+ = (m_camera.position-m_transform.position). Normalized*time.deltatime*move_speed;//Zombie LookAt camera and move to it } } voidOnMouseDown () {if(!Is_die) {M_rigidbody. Addforce (NewVector3 (force_x, force_y, force_z));//Add a force if zombie not deadStartcoroutine (Blood_out ());//play the blood animatorhp--; if(0==hp) Startcoroutine (Die ()); ElseStartcoroutine (Hit ()); }} IEnumerator Blood_out () {Transform T=Instantiate (blood). Transform; T.position=m_transform.position; Debug.Log (T.name); yield return NewWaitforseconds (1f);//The animator last for 1 secondsDestroy (T.gameobject); } IEnumerator die () {Is_die=true; yield returnStartcoroutine (Playanimation (clipname[4],1f)); yield return NewWaitforseconds (3f);//After 3 seconds,destory the dead zombieDestroy (M_transform.gameobject); } IEnumerator Hit () {Is_hit=true; yield returnStartcoroutine (Playanimation (clipname[6],0.2f)); Is_hit=false; } IEnumerator playanimation (stringNamefloatTime ) {m_animation. Play (name); yield return Newwaitforseconds (time); }}
Summarize today's unity face Question (a): The Click event of a rigid body