Unity People are several ways to move

Method One: transform.Translate

Prior to revisit the first to know the direction is forward Vector3.forward is the Z-axis, if you do not understand can look at the chart

Here Insert Picture Description

The world coordinate system moving

The code is written according to the z-axis direction moving world axes, and then look at what the world is found in the upper right corner axes according to the z-axis movement of the world axis is no problem, there is a problem in observing this object is itself z-axis are coordinate axes to the left side, since the effect of this method is the second parameter, Space.World, Space.Self itself is moved according to the direction of movement according to the World coordinate axes
	// Update is called once per frame
	void Update () {
        transform.Translate(Vector3.forward * Time.deltaTime * 10f, Space.World);
    }

Here Insert Picture Description

Own mobile case

The next line comment line is equivalent to the previous line, this method has a default enumeration is carried out according to their displacement coordinate system, that is to say write here do not write Space.self are the same.

	// Update is called once per frame
	void Update () {
        transform.Translate(Vector3.forward * Time.deltaTime * 10f,Space.Self); //1
        //transform.Translate(Vector3.forward * Time.deltaTime * 10f); //2
    }

Look at the results:
Here Insert Picture Description

Method two: CharacterController.Move (vector dir)

CharacterController.Move this method does not tell us explicitly enumerated in the world coordinate system or their own coordinate system, so there is usually transform.XXX (forward) to represent their coordinates, with Vector.XXX (forward) to represent the world coordinates.

As shown below:

Move in the world axes:

Here Insert Picture Description
code show as below:

	// Update is called once per frame
	void Update () {
        chars.Move(Vector3.forward * Time.deltaTime * 10f);
    }

Move according to its own axis:

Here Insert Picture Description
code show as below:

	// Update is called once per frame
	void Update () {
        chars.Move(transform.forward * Time.deltaTime * 10f);
    }

Method three: CharacterController.SimpleMove (vector dir) with caution

Why do I say this with caution because this method is the need to move to some kind of foundation can be moved
1. First need to make a ground-moving objects on it, if not then do not play any role
2. This method is built rigid body, with rigid free-fall effect when used, there is no free-fall effect of a rigid body if you do not use
3. This method is now two ways to speed mechanism I described above is not the same, do not need to use * Time.delatetime

FIG effect on the following:

Move in the world axes:

Here Insert Picture Description

code show as below:

	void Update () {
        chars.SimpleMove(Vector3.forward);
    }

Move according to its own axis:

Here Insert Picture Description

code show as below:

	void Update () {
        chars.SimpleMove(transform.forward);
    }
Published 62 original articles · won praise 5 · Views 3899

Guess you like

Origin blog.csdn.net/qq_42194657/article/details/103908957