Skip to content

Instantly share code, notes, and snippets.

@yumayanagisawa
Last active December 5, 2017 10:47
Show Gist options
  • Save yumayanagisawa/ebed56abb2a737f198e8b813dcf76a0d to your computer and use it in GitHub Desktop.
Save yumayanagisawa/ebed56abb2a737f198e8b813dcf76a0d to your computer and use it in GitHub Desktop.
Unity3D | Swarm-like Animation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationController : MonoBehaviour {
private float speed;
private Vector3 destination = new Vector3(10, 20, 1);
private Vector3 initialTargetPosition;
private bool isInitialMotion = true;
private Vector3 initialDirection;
private Vector3 initialMass = new Vector3(0, 0, 3);
private Vector3 secondDirection;
private Vector3 newMass;
// Use this for initialization
void Start()
{
// calculate the vector of the second direction
secondDirection = (destination - initialMass).normalized;
speed = Random.value * 100f + 100f;
float x = Random.value - 0.5f;
float y = Random.value - 0.5f;
float z = Random.value - 0.5f;
//z += 15.0f;
initialTargetPosition = new Vector3(x, y, z);
initialTargetPosition.Normalize();
initialTargetPosition *= Random.Range(0.4f, 1.0f);
initialTargetPosition.z += 3.0f;
initialDirection = (initialTargetPosition - transform.position).normalized;
}
// Update is called once per frame
void Update()
{
if (Time.realtimeSinceStartup > 30.0f)
{
transform.position = Vector3.MoveTowards(transform.position, destination, speed * 0.0001f);
//transform.RotateAround(transform.position, Vector3.one, speed * Time.deltaTime);
//
initialMass = Vector3.MoveTowards(initialMass, destination, speed * 0.0001f);
transform.RotateAround(initialMass, initialDirection, speed * Time.deltaTime);
}
else if (!isInitialMotion)
{
transform.RotateAround(initialMass, initialDirection, speed * Time.deltaTime);
}
else if (Vector3.Distance(initialTargetPosition, transform.position) < 0.1f)
{
transform.RotateAround(initialMass, initialDirection, speed * Time.deltaTime);
isInitialMotion = false;
}
else
{
transform.position = Vector3.MoveTowards(transform.position, initialTargetPosition, speed * 0.0003f);
transform.RotateAround(transform.position, Vector3.one, speed * Time.deltaTime);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InitPrefab : 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;
//z += 15.0f;
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 () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment