In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Unity to draw the trajectory of an object's motion". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Unity to draw the trajectory of an object in motion.
In this paper, an example is given to share the specific code for drawing the trajectory of an object in Unity for your reference. The details are as follows.
1. Create a new empty object with LineRenderer on it
2. Create new empty objects, draw tracks, design and script.
3. The script for LineMark is
Using System.Collections;using System.Collections.Generic;using UnityEngine; public class LineMark: MonoBehaviour {private GameObject clone; private LineRenderer line; private int i; public GameObject obs; public GameObject run; Vector3 RunStart; Vector3 RunNext; / / Use this for initialization void Start () {RunStart = run.transform.position; clone = (GameObject) Instantiate (obs, run.transform.position, run.transform.rotation); / / Clone an object with LineRender line = clone.GetComponent () / / get the LineRender component on the object / line.SetColors (Color.blue, Color.red); / / set the color / line.SetWidth (0.2f, 0.1f); / / set the width I = 0;} / / Update is called once per frame void Update () {RunNext = run.transform.position; if (RunStart! = RunNext) {iTunes; line.SetVertexCount (I) / / set the number of vertices line.SetPosition (iMur1, run.transform.position);} RunStart = RunNext; / / if (Input.GetMouseButtonDown (0)) / / {/ / clone = (GameObject) Instantiate (obs, obs.transform.position, transform.rotation); / / Clone an object with LineRender / / line = clone.GetComponent (); / / get LineRender components on the object / / line.SetColors (Color.blue, Color.red) / / set color / / line.SetWidth (0.2f, 0.1f); / / set width / / I = 0; / / print ("GetMouseButtonDown"); / /} / / if (Input.GetMouseButton (0)) / / {/ / line.SetVertexCount (I); / / set the number of vertices / / line.SetPosition (I-1, Camera.main.ScreenToWorldPoint (Input.mousePosition.x, Input.mousePosition.y, 15)) / / set vertex position / / print ("GetMouseButton"); /}
4. Sports balls and scripts
Run.cs
Using UnityEngine;using System.Collections; public class Run: MonoBehaviour {public GameObject target; / / the target to be reached public float speed = 10; / / the distance between the two private bool move = true; void Start () {/ / calculate the distance between the two distanceToTarget = Vector3.Distance (this.transform.position, target.transform.position); StartCoroutine (StartShoot ()) } IEnumerator StartShoot () {while (move) {Vector3 targetPos = target.transform.position; / / keep it facing the target this.transform.LookAt (targetPos); / / calculate the angle in the arc float angle = Mathf.Min (1, Vector3.Distance (this.transform.position, targetPos) / distanceToTarget) * 45; this.transform.rotation = this.transform.rotation * Quaternion.Euler (Mathf.Clamp (- angle,-42,42), 0,0) Float currentDist = Vector3.Distance (this.transform.position, target.transform.position); if (currentDist < 0.5f) move = true; this.transform.Translate (Vector3.forward * Mathf.Min (speed * Time.deltaTime, currentDist)); yield return null;}
5. Scripts for target balls and motion settings
Follew.cs
Using System.Collections;using System.Collections.Generic;using UnityEngine; public class followme: MonoBehaviour {Rigidbody follew; / / Use this for initialization void Start () {follew = GetComponent ();} / / Update is called once per frame void Update () {transform.Translate (new Vector3 (0.1fMagne 0.1f));}}
6. Running result
At this point, I believe you have a deeper understanding of "how to use Unity to draw the trajectory of an object's motion". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.