Skip to content

Instantly share code, notes, and snippets.

@yumayanagisawa
Created December 4, 2017 23:51
Show Gist options
  • Save yumayanagisawa/2521e07775584e3625afea3f12861787 to your computer and use it in GitHub Desktop.
Save yumayanagisawa/2521e07775584e3625afea3f12861787 to your computer and use it in GitHub Desktop.
Unity3D | Rotate around objects
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
public Transform prefab;
// Use this for initialization
void Start () {
for (int i = 0; i < 300; i++)
{
float x = Random.value - 0.5f;
float y = Random.value - 0.5f;
float z = Random.value - 0.5f;
Vector3 thisPos = new Vector3(x, y, z);
thisPos *= 5.0f;
thisPos.x += -15;
thisPos.y += -15;
thisPos.z += -15;
Transform temp = Instantiate(prefab, thisPos, prefab.rotation);
}
}
// Update is called once per frame
void Update () {
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateAround : MonoBehaviour {
private float speed;
private Vector3 destination = new Vector3(10, 20, 1);
private Vector3 initialTargetPosition;
private bool isInitialMotion = true;
private Vector3 initialDirection;
// Use this for initialization
void Start () {
speed = Random.value * 100f + 100f;
float x = Random.value - 0.5f;
float y = Random.value - 0.5f;
float z = Random.value - 0.5f;
initialTargetPosition = new Vector3(x, y, z);
initialTargetPosition *= 5.0f;
initialDirection = (initialTargetPosition - transform.position).normalized;
}
// Update is called once per frame
void Update () {
if (Time.realtimeSinceStartup > 25.0f)
{
transform.position = Vector3.MoveTowards(transform.position, destination, speed * 0.001f);
transform.RotateAround(transform.position, Vector3.one, speed * Time.deltaTime);
}
else if (Time.realtimeSinceStartup > 5.0f)
{
transform.RotateAround(Vector3.zero, Vector3.one, speed * Time.deltaTime);
} else
{
transform.position += initialDirection * 7.1f * Time.deltaTime;
transform.RotateAround(transform.position, Vector3.one, speed * Time.deltaTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment