Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use unity3D to realize 3D object following Mouse

Shulou Source: shulou.com Published: 2022-06-03 05:58:17 09月16日 Update

Today, I will talk to you about how to use unity3D to achieve three-dimensional objects to follow the mouse, many people may not understand, in order to make you better understand, the editor summed up the following, I hope you can get something according to this article.

For everyone to share the unity3D implementation of three-dimensional objects to follow the mouse specific code, for your reference, the specific content is as follows

Effect picture:

1. Create a 3D object Cylinder cylinder Target

two。 Create a glossary Yellow shader

3. Attach a shader to a Target object

4. Create script MouseMove.cs

MouseMove.cs code

Using UnityEngine;/* makes the object follow the mouse completely * / public class MouseMove: MonoBehaviour {[Header ("main camera")] [Tooltip ("main camera")] / main camera / public Camera maincamera; [Header ("target object")] [Tooltip ("target object")] / target object / public Transform target; void Start () {isDown = false TargetWTSP = m_mainCamera.WorldToScreenPoint (m_target.position);} / whether to press / private bool isDown; / record the position of target world coordinates to screen coordinates during initialization WTSP==WorldToScreenPoint / private Vector3 targetWTSP; void Update () {if (Input.GetMouseButtonDown (0)) {isDown = true;} if (Input.GetMouseButtonUp (0)) {isDown = false }} private void LateUpdate () {if (isDown) {Vector3 mouseVec3 = m_mainCamera.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, targetWTSP.z); m_target.position = mouseVec3;}

5. Create a new empty object Script, mount the MouseMove.cs script, and then drag the camera and Target objects onto the script

Over!

After reading the above, do you have any further understanding of how to use unity3D to achieve 3D objects following the mouse? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Object 3D content camera mouse target script code coordinates time material world position cylinder cylinder screen effects effect more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Shulou Technology Shulou Tech Info Linux