[Unity Base] moves the object toward the camera's viewing direction (i)

Source: Internet
Author: User

Scenario One: Suppose there was a man who was facing the north, he had a camera behind him, and it happened to be facing the north. Now, he turns left 90 degrees, facing the West. At this point, if you let him go forward, then there are two situations.


Situation one: Move forward in its own coordinate system. He will go straight to the eye, in fact he is going to the West, the camera sees that he is to "right" drip.

Situation two: Go forward in the world coordinate system. He will go to the north, and the camera sees him as a "front" drop.

Thus, if the object is rotated, then moving in its own coordinate system will often be wrong, so in the game the movement of the characters are generally in the world coordinate system to move the drop. Moving in a world coordinate system does not take into account the rotation of the object itself.

So it's often found in the Code of Character movement:

Transform.transformdirection
From its own coordinates to the world coordinate transformation direction.
Transform.forward
The blue axis of the world Space coordinate transformation. That is, the z axis.

Which transform. Transformdirection (New Vector3 (0, 0, 1)); The shorthand method is transform.forward;


Scenario Two: Suppose there was a man who, facing the north, had a camera on his left hand side.

At this point if he goes forward in the world coordinate system. He will go to the north, the camera sees him as "left". This is wrong and if you want the character to move forward, the character moves eastward. Visible, at this time the "front" is from the camera angle, so it is necessary to consider the camera rotation angle.

Our goal is to let the character "move along the camera angle," from the code point of view, is: Quaternion*vector3, OK, give us the code directly!


Using unityengine;using System.collections;public class Playercontrol:monobehaviour {public        float movespeed = 2f;< c1/>public float rotatespeed = 2f;void Update () {        float h = input.getaxis ("horizontal");        Float v = input.getaxis ("Vertical");        if (h! = 0 | | V! = 0)        {            Vector3 targetdirection = new Vector3 (h, 0, v);            Float y = camera.main.transform.rotation.eulerangles.y;            targetdirection = Quaternion.euler (0, y, 0) * targetdirection;            Transform. Translate (targetdirection * time.deltatime * movespeed,space.world);        }        if (Input.getkey (KEYCODE.J))        {            transform. Rotate (-vector3.up * time.deltatime * rotatespeed);}}}        


[Unity Base] moves the object toward the camera's viewing direction (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.