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

How to use unity3D to realize 3D object following Mouse

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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.

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

Development

Wechat

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

12
Report