Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Unity3 mouse click move

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This can be divided into three parts:

First: how to get the target location

Second: look at the target location and move to it

Third: obstacle judgment

Let's first look at the first question how to get the target point: first, open unity3d and create a new project file.

Create a Cube to resize, make it into a ground form, and change the name to "floor" to adjust the camera to the right angle.

Create a C # script named "Pathfinding".

Enter the following code in the script:

Void Update ()

{

/ / when the stand-alone mouse

Void Update ()

{

If (Input.GetMouseButtonDown (0))

{

/ / define a shot from the main camera to the mouse position

Ray ray=Camera.main.ScreenPointToRay (Input.mousePosition)

RaycastHit hit

/ / determine whether the rays collide or not

If (Physics.Raycast (ray, out hit, 100))

{

/ / determine whether the collision object is floor

If (hit.collider.gameObject.name== "floor")

{

/ / print out the coordinates of the collision point

Debug.Log (hit.point)

}

}

}

}

Add the code to our camera object, run the game, click the mouse on the ground we created, and the console will output the coordinates of the click point

Through the simple example above, we can get the coordinates of the destination point: when we click the mouse, we emit a ray from our camera in the direction of our mouse, and when the ray collides with the ground, output the coordinates of the collision point, which is the coordinates of the point where the mouse clicks to the ground, that is, the coordinates of the target point.

The coordinate point has been found, and the next one is to make our game object look at this coordinate point.

There is a simple way is to directly let our protagonist lookat this coordinate point.

Let's create a new game object as our protagonist and change the name to "person".

The supplementary script is as follows:

Private GameObject play

Void Start ()

{

Play=GameObject.Find ("person")

}

/ / Use this for initialization

Void Update ()

{

If (Input.GetMouseButtonDown (0))

{

Ray ray=Camera.main.ScreenPointToRay (Input.mousePosition)

RaycastHit hit

If (Physics.Raycast (ray, out hit, 100))

{

If (hit.collider.gameObject.name== "floor")

{

Debug.Log (hit.point)

Play.transform.LookAt (hit.point)

}

}

}

}

Run the game, and when we click on the ground, the game object rotates and looks at the point we click on.

There is a problem with this, the protagonist will instantly rotate to the angle we need, we now have to control his rotation speed and let it rotate slowly.

This requires a different approach: the quaternion transform.rotation = Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed)

From.rotation is the initial angle

To.rotation is the target angle

Speed is the speed of rotation

By using a method, we can make the object rotate slowly to the specified angle we want.

Specifically, create a new empty object, name it To_rotation, and make it a child of the person object.

The code is supplemented as follows:

Public Transform to

Public float speed = 5F

Private GameObject play

Void Start ()

{

Play=GameObject.Find ("person")

To=GameObject.Find ("To_rotation")

}

/ / Use this for initialization

Void Update ()

{

If (Input.GetMouseButtonDown (0))

{

Ray ray=Camera.main.ScreenPointToRay (Input.mousePosition)

RaycastHit hit

If (Physics.Raycast (ray, out hit, 100))

{

If (hit.collider.gameObject.name== "floor")

{

Debug.Log (hit.point)

/ / dissolve the to father-son relationship after clicking on the ground, and let me lookat the target point

To.transform.parent = null

To.LookAt (hit.point)

}

}

}

/ / if the rotation angle of our play is the same as that of to, restore its parent-son relationship and return its coordinates to zero

If (play.transform.rotation==to.rotation)

{

To.transform.parent=play.transform

To.transform.localPosition = new Vector3 (0,0,0)

}

Else

/ / if the angle of play is different from that of to, rotate play to the angle of to

Play.transform.rotation = Quaternion.Slerp (play.transform.rotation, to.rotation, speed)

}

Execute the code to get the effect we want

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report