Today, we study the model controller and explore how to implement the basic control model method.
First, I pulled a few small models.
This is the pig's foot. First add the usual before and after, that is, Wsad, press Wsad to make the role of transform component changes under the position,
private float movespeed = 3f; void Start () { } void Update () { if (Input.getkey (KEYCODE.A)) {
Change the role x axis transform. Translate (vector3.right *-movespeed * time.deltatime, Space.world); } if (Input.getkey (KEYCODE.D)) { transform. Translate (vector3.right * movespeed * time.deltatime, Space.world); } if (Input.getkey (KEYCODE.W)) {
Change the role y -axis transform. Translate (Vector3.forward * movespeed * time.deltatime, Space.world); } if (Input.getkey (KEYCODE.S)) { transform. Translate (Vector3.forward *-movespeed * time.deltatime,space.world); } }
Ah, now that ORC should be able to move around, and here is by changing the character of the transform position to achieve (in fact, is nonsense, all through change this, you know I mean good),
In a different way, I'm going to give a target, let the character pan over.
private float movespeed = 6f; void Update () { Vector3 targetpos = transform.position; if (Input.getkey (KEYCODE.A)) { targetpos.set (targetpos.x-movespeed * time.deltatime,targetpos.y, TARGETPOS.Z); } if (Input.getkey (KEYCODE.D)) { targetpos.set (targetpos.x+movespeed * time.deltatime,targetpos.y, TARGETPOS.Z); } if (Input.getkey (KEYCODE.W)) { Targetpos.set (targetpos.x,targetpos.y,targetpos.z+movespeed * time.deltatime); } if (Input.getkey (KEYCODE.S)) { Targetpos.set (targetpos.x,targetpos.y,targetpos.z-movespeed * time.deltatime); } Transform.position = Vector3.lerp (transform.position, Targetpos, Movespeed * time.deltatime); }
This is two ways to move, temporarily only think of two ways of moving, if there are other replies to me.
In addition to moving the way, the direction is also to consider, in the first way of moving, how to set the orientation of the role? I think so, get the position before the move and the position after the move, subtract to get direction, ah, I was so smart, hands
private float movespeed = 3f; void Start () { } void Update () { //Get pre-move position Vector3 lastposation = transform.position; if (Input.getkey (KEYCODE.A)) { transform. Translate (vector3.right *-movespeed * time.deltatime); } if (Input.getkey (KEYCODE.D)) { transform. Translate (vector3.right * movespeed * time.deltatime); } if (Input.getkey (KEYCODE.W)) { transform. Translate (Vector3.forward * movespeed * time.deltatime); } if (Input.getkey (KEYCODE.S)) { transform. Translate (Vector3.forward *-movespeed * time.deltatime); } Sets the direction transform. LookAt (transform.position+ transform.position-lastposation); }
Then, the veteran should lol
See no effect, if according to my code, the villain will be in a circle in situ, looks very idiot.
........
The ellipsis means I've been thinking about it for a long time, and then I saw Unityapi.
public class Example:monobehaviour {void Update () {transform. Translate (Vector3.forward * time.deltatime); transform. Translate (Vector3.up * time.deltatime, Space.world);}}
Originally is the coordinate system question, Space.world, but I originally did not consider this question ah, seems can also realize the movement?
Think of the will, the original move, the Beast face is the same direction. So he moves without considering the direction of the line. Now the orc's direction is changing, and its own coordinate system is changing, so add the world coordinate system
So the orc didn't have to take the space step at last.
The second method of moving how to do the direction, should just look at the target point on the line, review the moving code, unlike its own coordinate system, add
Transform. LookAt (Targetpos + targetpos-transform.position);
The key event to get AWSD here can be replaced by Unity's API, Input.getaxis ()
private float movespeed = 3f; void Start () {} void Update () {//Get pre-move position Vector3 lastposation = transform.position; Float h = input.getaxis ("horizontal"); Float v = input.getaxis ("Vertical"); Transform. Translate (New Vector3 (h, 0, v) * movespeed * time.deltatime,space.world); /* IF (Input.getkey (KEYCODE.A)) {transform. Translate (vector3.right *-movespeed * time.deltatime, Space.world); } if (Input.getkey (KEYCODE.D)) {transform. Translate (vector3.right * movespeed * time.deltatime, Space.world); } if (Input.getkey (KEYCODE.W)) {transform. Translate (Vector3.forward * movespeed * time.deltatime, Space.world); } if (Input.getkey (KEYCODE.S)) {transform. Translate (Vector3.forward *-movespeed * time.deltatime,space.world); } * *//Set Direction transform. LookAt (transform.position+ Transform.posItion-lastposation); }
Instantly feel refreshed, Virgo's gospel, and can be found that the angle is not the same as the original only 45 degrees angle change, moving also smooth, in short, the waist does not ache, leg not sour, can on six floor
Input.getkey () method can get the input class of mouse and touch screen by different parameters, in fact I have this method, and similar invoke () method, through the string call other methods I am very interested in, will go to see this method of implementation principle and merits.
Common PC game operation mode and mouse, I came to see how the mouse click is how to move the mouse to monitor his right click on the time, this point is a three-dimensional coordinates, but the screen is 2d, I think for a while, combined with my years of game experience,
He mouse point of the place at the end are on the ground there is wood, so here should be to get to the ground, I do not understand the mouse click Point is 2d how to convert to 3D, first exclude add default y=0 (X,0,z), and then everyone please see
Although a bit abstract, but revealed a thick post-modern art breath, A is the screen, B is the virtual ground, C is the mouse point, did c on a side of the normal intersection C surface point should be clicked on the ground point, whether it is parallel or cross the game,
Normals should all be applied . First look at the mouse right click will get what it.
As a result, nothing's going to happen, Dad. Let's see what the API says.
using UnityEngine;using System.Collections;public class example : MonoBehaviour {public float horizontalSpeed = 2.0F;public float verticalSpeed = 2.0F;void Update() {float h = horizontalSpeed * Input.GetAxis("Mouse X");float v = verticalSpeed * Input.GetAxis("Mouse Y");transform.Rotate(v, h, 0);}}
// Performs a mouse look.//执行一个鼠标观察var horizontalSpeed : float = 2.0;var verticalSpeed : float = 2.0;function Update () {// Get the mouse delta. This is not in the range -1...1//获取鼠标增量,范围不在-1...1var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");transform.Rotate (v, h, 0);}
JavaScript has a comment, and is a parent son. See comment No, get mouse increment. Then I scratched the screen with my mouse. And see the changes in the parameters and the angle of the model
This value is sometimes seen in hindsight, in the spirit of scientific research, I carried out a careful study of the control variable method , and found that x and Y have data to be quickly mouse-strokes, and there is
data will be called after update three times. The truth of the matter, see the unity of the night.
OK, now uncover, this xy is really a mouse in a frame of the slide vector, and call Update three times because the hand shook: D Crossing, I am also the victim, do not spit groove.
Although it took some time here, but it is not to do without work, at least after the rotation of the lens is a way to do.
I think the vector here is to get a frame before the mouse position and a frame after the mouse position, and then get the vector, a frame of time is very short, can be considered in a frame almost impossible to double-click to cause a miscarriage of, of course, you can also be in the
to judge the mouse when double-click.
In some games mouse click can trigger is the Click event is also willing to drag the time, like I want to click on the file, how to determine whether to drag or click, 1 to determine the mouse click and release position 2. Determine the mouse click and release of the time
thought would be here is obviously time more reasonable Ah, For example, the mouse dragged out and dragged back in place this is obviously 1 not satisfied. I can only think of time temporarily.
As to why I ask these two questions, because the difference between a drag and a machine is the position and time of the mouse, please call me Colin Doyle Ben.
Speaking of Here, the previous Input.getaxis method to get the keyboard is also the gain increment, the keyboard always can not get the position on the screen, it is estimated to be unified so the mouse is also obtained by the increment. The
pulled a long way, or did not find the point when the mouse clicked. Since Input.getaxis ("Mouse Y") This method can obtain the vector, that should also have the method of acquiring position, always can not let us listen to the serial port to go.
Looking for Inputapi, I saw this input.mouseposition, with my four level 430 high-segmented contestants, this must be the position of the mouse, we Debug.Log (input.mouseposition) out the position of the mouse to see
These points seem to be the position of the screen with a Z axis of 0, and I'll point a few more points, four corners and the middle of the try
As can be seen, the position of this point is the bottom left corner as the origin, in pixels as coordinates, such as I am 1920*1080 's screen, and this coordinate system is very consistent.
How do you get the mouse position on the screen and project it into the scene? Again, please, abstract one and second.
In the first picture, A is a display B is a scene. The second picture of the red box is a display, note that the camera, the screen equivalent to the middle of the interception of one side, that a point is equivalent to the mouse point, the screen is the camera and a point of the ray, see things, a point coordinates we get to is mouseposition, but this coordinate is the relative coordinates, is relative to the lower left corner of the two-dimensional coordinates, depending on the location of the camera to obtain the camera's world coordinate point
In the more second row angle, rotation, you can get a cone, you can see the above abstract 2, the screen should be in the middle of a fixed position or 1/3 or other, combined with the mouse point to calculate the point of the world coordinate system, and then 2.1 lines to make the ray,
Then judge the impact point. This is where we point to the scene on the screen. So many words said I would not go to write the wheel, Unityapi there is such a deal
if (Input.getmousebutton (0)) { Debug.Log (input.mouseposition); Get the point on the screen with the camera's Ray ray Ray = Camera.main.ScreenPointToRay (input.mouseposition); Raycasthit hit; Gets the collision point if (Physics.raycast (Ray,out hit)) { //debug Debug.Log (hit.point); } Let the character look at this point transform. LookAt (Hit.point); }
The Screenpointtoray method is to get the camera and mouse point of the ray. No, not exactly.
Unity Learning--003: Role Controllers