Article

Published a year ago
242
0

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

public class CreatePointsAround : MonoBehaviour {

public int _count;
public GameObject _obj;

public List<GameObject> _objGroup;

private float eachAngle;

// Use this for initialization
void Start()
{
eachAngle = 360f / _count;
MakePoints();
}

public void MakePoints()
{
//生成第一圈对象

for (float i = 1;i< _count / 4f; i++)
{
//获取下一个圆与球心距离以及其半径
//向上生成
//向下生成
}

}

/// <summary>
/// 生成一圈对象
/// </summary>
/// <param name="yPosition"></param>
public void MakePointsByCircle(float yPosition, float thisRadius)
{
Vector2 firstPoint = new Vector2(0, thisRadius);
Vector2 nextPoint = firstPoint;
for (int i = 0; i < _count; i++)
{
//生成新对象
GameObject createdPoint = Instantiate(_obj, Vector3.zero, Quaternion.identity, this.transform);
//向对象组添加新生成的对象
createdPoint.name = "obj" + i;
createdPoint.tag = "objAround";

//这里用一个脚本操控对象慢慢位移到目标点（不知道为啥canvas直接instantiate坐标会有问题）
createdMove.positionTo = new Vector3(nextPoint.x, yPosition, nextPoint.y) + this.transform.position;
//获取下一个点
}
}

/// <summary>
/// 获取当前圆上下一个点的坐标
/// </summary>
/// <param name="thisPoint">当前点坐标</param>
/// <param name="eachAngle">每两个相邻点与圆心连线之间的夹角</param>
/// <returns></returns>
{
Vector2 nextPoint = new Vector2();
float thisAngle = getThisAngle(thisPoint);

//下一个点和初始点与圆心连线的夹角
float nextAngle = thisAngle + eachAngle;

//Debug.Log(nextAngle);
//获取下一个点的坐标
return nextPoint;
}

/// <summary>
/// 获取点所在半径与初始点所在半径的夹角（0~360°）
/// </summary>
/// <param name="thisPoint">当前点坐标（忽视y轴）</param>
/// <returns>返回0~360°的夹角</returns>
float getThisAngle(Vector2 thisPoint)
{
float thisAngle;
if (thisPoint.x >= 0 && thisPoint.y > 0)
{
thisAngle = Mathf.Rad2Deg * Mathf.Atan(thisPoint.x / thisPoint.y);
}
else if (thisPoint.x > 0 && thisPoint.y <= 0)
{
thisAngle = -Mathf.Rad2Deg * Mathf.Atan(thisPoint.y / thisPoint.x) + 90;
}
else if (thisPoint.x <= 0 && thisPoint.y < 0)
{
thisAngle = Mathf.Rad2Deg * Mathf.Atan(thisPoint.x / thisPoint.y) + 180;
}
else if (thisPoint.x < 0 && thisPoint.y >= 0)
{
thisAngle = -Mathf.Rad2Deg * Mathf.Atan(thisPoint.y / thisPoint.x) + 270;
}
else
{
thisAngle = 0;
}
//Debug.Log(thisAngle);
return thisAngle;
}
}

``````

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

public class LerpToPosition : MonoBehaviour {

public int _maxLerpCount = 100;

[HideInInspector]
public Vector3 positionTo;

private bool doLerp;
private int lerpCount;

// Use this for initialization
void Start () {
scroll thisScrollScript = this.GetComponent<scroll>();
if(thisScrollScript != null)
{
thisScrollScript.enabled = false;
}
doLerp = true;
}

// Update is called once per frame
void Update () {
if (doLerp)
{
this.transform.position = Vector3.Lerp(this.transform.position, positionTo, 0.1f);
lerpCount++;
if(lerpCount >= _maxLerpCount)
{
doLerp = false;

scroll thisScrollScript = this.GetComponent<scroll>();
if (thisScrollScript != null)
{
thisScrollScript.enabled = true;
}
}
}
}
}

``````

3