Unity3d allows you to move a role to a specified location by clicking the terrain with the left mouse button [script]

Source: Internet
Author: User
It involves moving speed, main character object, target point, and Ray collision !~ [Javascript] varmoveSpeed: int5; // player movement speed varplayer: Transform; // defines the Transformprivatevarendposition: Vector3; fun... it involves moving speed, main character object, target point, and Ray collision! ~ [Javascript] var moveSpeed: int = 5; // player movement speed var player: Transform; // defines the Transform private var endposition of a character: Vector3; function Start () {endposition = player. transform. position;} function Update () {if (Input. getButtonUp ("LeftMouse") {// The LeftMouse is set in inputManager and the left-key value is mouse 0 PlayerMove ();} var targetposition = player. transformPoint (Vector3 (0, 48.8,-30); transform. position = targetposition; // the camera's Target location. The role of these two codes is to keep the character in the camera's field of view if (endposition! = Player. transform. position) {www.2cto.com player. position = Vector3.MoveTowards (player. position, endposition, Time. deltaTime * moveSpeed);} function PlayerMove () {var cursorScreenPosition: Vector3 = Input. mousePosition; // The Position of the mouse on the screen var ray: Ray = Camera. main. screenPointToRay (cursorScreenPosition); // emits a Ray at the position of the mouse's screen (the ray is X-ray now) var hit: RaycastHit; if (Physics. raycast (ray, hit) {if (hit. collider. gameObject. tag = "Terrain") {// set the Terrain Tag to Terrain endposition = hit. point ;}}}

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.