这将在同一位置实例化预制件10次。我希望在对象之间增加相同的差距。
预制件应该在绘制的圆上实例化。 我想在里面:
if (moveInCircles)
圈入所有新预制件。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotate : MonoBehaviour
{
[Header("Spin")]
public bool spin = true;
public Vector3 Direction;
[Range(0, 300)]
public float speed = 10f;
public bool randomSpeed = false;
public bool randomDirection = false;
[Range(0f, 100f)]
public float timeDirChange;
public Vector3 defaultDirection = new Vector3(1, 0, 0);
[Space(5)]
[Header("Move in circles")]
public bool moveInCircles = false;
public GameObject rotateAroundTarget;
public Vector3 axis;//by which axis it will rotate. x,y or z.
public float rotationSpeed; //or the speed of rotation.
public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
[Range(5, 50)]
public float radius = 5;
private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness;
private float nextRotationTime = 0f;
private int counter = 0;
public DrawCircle dc;
public GameObject prefab;
public int numOfObjects;
public int gap = 3;
// Use this for initialization
void Start()
{
dc.xradius = radius;
if (prefab != null)
{
for (int i = 0; i < numOfObjects; i++)
{
Instantiate(prefab);
var v = Quaternion.AngleAxis(Time.time * speed, Vector3.up) * new Vector3(radius, 0, 0);
//prefab.transform.position = rotateAroundTarget.transform.position + v;
prefab.transform.position = rotateAroundTarget.transform.position + v + new Vector3(rotateAroundTarget.transform.position.x * gap,0, 0);
}
}
}
private void Update()
{
if (randomSpeed)
{
speed = UnityEngine.Random.Range(0, 300);
}
if (spin)
{
if (randomDirection == false)
{
nextRotationTime = 0;
timeDirChange = 0;
Direction = defaultDirection;
}
else
{
if (Time.time > nextRotationTime)
{
nextRotationTime += timeDirChange;
RandomDirection();
}
}
transform.Rotate(Direction, speed * Time.deltaTime);
}
else
{
timeDirChange = 0;
randomDirection = false;
randomSpeed = false;
}
if (moveInCircles)
{
var v = Quaternion.AngleAxis(Time.time * speed, Vector3.up) * new Vector3(radius, 0, 0);
transform.position = rotateAroundTarget.transform.position + v;
}
dc.xradius = radius;
}
private void RandomDirection()
{
Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));
while (Direction == new Vector3(0, 0, 0))
{
counter++;
Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));
if (counter == 2)
{
Direction = new Vector3(1, 0, 0);
break;
}
}
counter = 0;
}
}
答案 0 :(得分:2)
尝试prefab.transform.position = rotateAroundTarget.transform.position + v + new Vector3(i, 0, 0);