Moving objects in Unity-transform.translate

Source: Internet
Author: User

Transform.translate is moved by setting the vector direction and size of the next move.

There are two ways to move:

1.ADWS control Game object Move up and down, do not involve rotation;

The 2.AD is controlled by the rotation control direction, the WS control moves before and after, also can realize the controlled displacement.

Either way, you get the keyboard response first, you can get it through input.getkey, or you can get it through Input.getaxisraw.

The code in the first way is as follows:

floatInput_h = Input.getaxisraw ("Horizontal"); Gets the direction of movement in the x direction, if input a, output-1; If you enter D, Output 1. floatInput_v = Input.getaxisraw ("Vertical");                Gets the direction of movement of the z direction, if input w, output 1, if input s, output-1. Vector3 v=NewVector3 (Input_h,0, Input_v); New Move Vector v=v.normalized; If it is a diagonal direction, it needs to be standardized, the uniform length is 1v= v * Speed *Time.deltatime;                       Multiply speed to adjust movement speed, multiply deltatime to prevent transform.translate (v); Moving

The code in the second way is as follows:

float Input_h = Input.getaxisraw ("horizontal");  float input_v = Input.getaxisraw ("Vertical");   Transform. Rotate (New Vector3 (0, Input_h, 0));  Rotation around the y axis, a key clockwise; D key counterclockwise float curspeed = speed * Input_v * time.deltatime;  Transform. Translate (Transform.forward * curspeed,space.world);//move around the object in front and back, because forward is used, so specify the moving coordinate system as the global coordinate

The last line of code can also be changed to:

Transform. Translate (New Vector3 (0,0,curspeed));//By default moves along the z axis of the object, which is the front and back direction

All of the above code needs to be implemented in the Update method.

  

  

Moving objects in Unity-transform.translate

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.