您现在的位置是:首页 >学无止境 >unity物体运动经过特定点并绘出轨迹网站首页学无止境

unity物体运动经过特定点并绘出轨迹

天人合一peng 2023-05-22 12:00:02
简介unity物体运动经过特定点并绘出轨迹

经过线如果有圆滑可以参考

Unity物体运动时画出轨迹_天人合一peng的博客-CSDN博客

并修改里面的数值轨迹会有变化

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);

经过特定点时螺钉出现并有动画

 

1 空物体加linerender设置

 2 空物体设置调用linerender绘制线

 linemark.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
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);//克隆一个带有LineRender的物体   
		line = clone.GetComponent<LineRenderer>();//获得该物体上的LineRender组件  
		i = 0;
	}
 
	// Update is called once per frame  
	void Update () {  


		RunNext = run.transform.position;
 
		if (RunStart != RunNext) {
			i++;
			line.SetVertexCount(i);//设置顶点数 
			line.SetPosition(i-1, run.transform.position);
 
		}
 
		RunStart = RunNext;


	}  
}

3 运动小球

 cablelinerun.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class CableLineRun : MonoBehaviour
{

    public Transform cableRun;   //要到达的目标
    public Transform cableStart;   //要到达的目标

    public Transform target01;   //要到达的目标
    public Transform target02;   //要到达的目标
    public Transform target03;   //要到达的目标


    // 固定
    public GameObject GuDing01;
    public GameObject GuDing02;
    public GameObject GuDing03;




    // Start is called before the first frame update
    void Start()
    {



    }

    // Update is called once per frame
    void Update()
    {



        
        if (Input.GetKey(KeyCode.A))
        {
      
            cableRun.DOLocalMove(target01.localPosition, 5f).OnComplete(onCompleteTarget01);

        }

        if (Input.GetKey(KeyCode.B))
        {
      

        }
        
    }


    void onCompleteTarget01()
    {
            GuDing01.SetActive(true);
            // 旋转的同时移动
            var s= DOTween.Sequence();
            s.Append(GuDing01.transform.DOLocalMoveZ(40,3f));
            s.Join(GuDing01.transform.DOLocalRotate(new Vector3(0, 0, 1000),3f));

            cableRun.DOLocalMove(target02.localPosition, 5f).OnComplete(onCompleteTarget02);

    }

        void onCompleteTarget02()
    {
            GuDing02.SetActive(true);
            // 旋转的同时移动
            var s= DOTween.Sequence();
            s.Append(GuDing02.transform.DOLocalMoveZ(-36f,3f));
            s.Join(GuDing02.transform.DOLocalRotate(new Vector3(0, 0, 1000),3f));


            cableRun.DOLocalMove(target03.localPosition, 5f).OnComplete(()=>{

            GuDing03.SetActive(true);
            // 旋转的同时移动
            var s= DOTween.Sequence();
            s.Append(GuDing03.transform.DOLocalMoveZ(-190f,3f));
            s.Join(GuDing03.transform.DOLocalRotate(new Vector3(0, 0, 1000),3f));


            });
    }
}

4 start小球

 5 三个经过点

 6 三个经过点上的螺钉

 7 效果

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。