Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Unity to draw the trajectory of an object in motion

Shulou Source: shulou.com Published: 2022-06-03 15:20:27 09月30日 Update

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!

Tags: Object motion trajectory script content target between width ball point component color learning utility deeper code position interest angle instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Redmi Apple macOS