unity中cube做椭圆(或圆)运动且绘制轨迹

首先在cube上挂上LineRenderer组件,要是绘制圆就是w,h值一样,因为椭圆的参数方程是x=acosθ,y=bsinθ;
圆的参数方程是x=rcosθ,y=rsinθ;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class drawYuan : MonoBehaviour {
    public  List points;
    public LineRenderer line;
    float angle = 0;
    public float w = 8;
    public float h = 4;
   public float r=5;
    public void AddPoints()
    {
        Vector3 pt = transform.position;

        if (points.Count>0&&(pt-lastPoint).magnitude<0.1f)
        {
            return;
        }

        points.Add(pt);
        line.numPositions = points.Count;
        line.SetPosition(points.Count-1, lastPoint);        
    }

    public Vector3 lastPoint {
        get {
            if (points==null)
            {
                return Vector3.zero;
            }
            return (points[points.Count-1]);
        }
    }


    void FixedUpdate() {
        angle += Time.deltaTime/100;
  //绘制椭圆需要的点
        float x = w * Mathf.Cos(angle * Mathf.Rad2Deg);
        float y = h * Mathf.Sin(angle * Mathf.Rad2Deg);

  //绘制圆需要的点
      // float x =r * Mathf.Cos(angle * Mathf.Rad2Deg);
      //  float y = r * Mathf.Sin(angle * Mathf.Rad2Deg);
        transform.position = new Vector3(x, y, 0);

        AddPoints();
    }    
}

Paste_Image.png
Paste_Image.png
Paste_Image.png

你可能感兴趣的:(unity中cube做椭圆(或圆)运动且绘制轨迹)