Preparing resources
Here i download a character model from the Internet with a set of animations. There are standing, running, killing monsters, etc.
We are going to implement the character's front and rear left and back movement, the WSDA key on the keyboard, because there is no walking animation. Simply use the run instead!!
For the time being, let's not care about code redundancy. Because I am also a novice to learn. All in order to achieve the effect first.
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classModelmove:monobehaviour5 {6 7 Public floatSpeed =2;8 //Use this for initialization9 voidStart ()Ten { One A } - - //Update is called once per frame the voidUpdate () - { - - //Saveposition before move +Vector3 oldposition =transform.position; - if(Input.getkey (KEYCODE.W)) + { ATransform. Translate (Vector3.forward * time.deltatime *Speed , space.world); atAnimation. Play ("Run");//Start Running - - } - if(Input.getkey (KEYCODE.S)) - { -Transform. Translate (Vector3.back * time.deltatime *Speed , space.world); inAnimation. Play ("Run");//Start Running - to } + if(Input.getkey (keycode.a)) - { theTransform. Translate (Vector3.left * time.deltatime *Speed , space.world); *Animation. Play ("Run");//Start Running $ Panax Notoginseng } - if(Input.getkey (KEYCODE.D)) the { +Transform. Translate (vector3.right * time.deltatime *Speed , space.world); AAnimation. Play ("Run");//Start Running the + } - $ $ //Set role Orientation - /* - * LOOKAT (Vector3 worldposition); the * Explanation of the manual: Rotate the object, this forward vector points to the target's current position. - Wuyi * That's what I understand: the Assumptions: - * The vector before moving is v1 Wu * The vector after the move is V2 - * Then the current target object will rotate the direction vector pointing to the v2-v1. About * LookAt () method. To use the world coordinate system Space.world $ * Transform.position-oldposition: Direction before move-direction after move = target vector - * transform.position + transform.position-oldposition: Turn at current position - */ -Transform. LookAt (transform.position + transform.position-oldposition); A } +}
See here is not found. The characters are running away and getting smaller. Because here the character is running. The camera did not follow the running. So what? Here it is easy to have the camera move with the character to achieve the effect of roaming. Just drag the camera into the role object. As a sub-object of the role, the comparison effect is not obvious in the code above. We mentioned the LookAt method. What kind of a method is it? The above code is also explained, if you do not understand. Let's compare it to the next. When you move a role. Not using the LookAt method is the kind of effect that fancy. The left role is used LookAt, the right character is not used to understand the phrase: LookAt is to make: Rotate the object, this forward vector points to the current position of target (explained from the manual) just contact Unity3d shortly. But I have been deeply attracted to him. Come on. I'm going to be the Game Boy. This section resource model download
Http://pan.baidu.com/s/1ARiBO Key: Uqs8
unity3d-small case of the role of simple roaming