C # moving and limiting the moving distance,

Source: Internet
Author: User

C # moving and limiting the moving distance,

Public class PlaneMove: MonoBehaviour {

 

// H: horizontal direction control; v: backward direction control

Float h, v;

// Speed: aircraft Speed

Public float Speed;

// Use this for initialization

Void Start (){}

// Update is called once per frame

Void Update (){

// Horizontal value

H = Input. GetAxis ("Horizontal ");

// Vertical Value

V = Input. GetAxis ("Vertical ");

// Move one meter vertically per second

Transform. position + =-v * new Vector3 (0, 0, 1f) * Speed * Time. deltaTime;

// Move one meter per second horizontally

Transform. position + =-h * new Vector3 (1f, 0, 0) * Speed * Time. deltaTime;

// Limit the X axis-3.5 ~ 3.5, Z axis-10 ~ 1

Vector3 pos = transform. position;

Float x = pos. x;

Float z = pos. z;

X = Mathf. Clamp (x,-3.5f, 3.5f );

Z = Mathf. Clamp (z,-10f, 1f );

Pos. Set (x, pos. y, z );

Transform. position = pos;

}

}

Related Article

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.