Created
December 4, 2017 23:51
-
-
Save yumayanagisawa/2521e07775584e3625afea3f12861787 to your computer and use it in GitHub Desktop.
Unity3D | Rotate around objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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