Skip to content

Instantly share code, notes, and snippets.

@kitposition
Last active April 7, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitposition/174260c9c66e86feacfafd43b3c84b5d to your computer and use it in GitHub Desktop.
Save kitposition/174260c9c66e86feacfafd43b3c84b5d to your computer and use it in GitHub Desktop.
Unity-chan SpringBoneに風の影響を付加するスクリプト
using UnityEngine;
[RequireComponent(typeof(BoxCollider), typeof(Rigidbody))]
public class WindBlower : MonoBehaviour
{
public bool isWindActive = true;
public Vector3 windPower = new Vector3(0, 0, -100f);
public Vector3 Force { get; private set; }
private void Reset()
{
GetComponent<Rigidbody>().useGravity = false;
GetComponent<BoxCollider>().isTrigger = true;
}
void Update()
{
float Noise = Mathf.PerlinNoise(Time.time, 0.0f);
Force = isWindActive ? new Vector3(Noise * windPower.x, Noise * windPower.y, Noise * windPower.z) : Vector3.zero;
}
private void OnDrawGizmos()
{
BoxCollider collider = GetComponent<BoxCollider>();
Color color = isWindActive ? Color.blue : Color.grey;
Gizmos.color = color;
Gizmos.DrawLine(transform.position, transform.position + windPower * 0.01f);
Gizmos.color = new Color(color.r, color.g, color.b, 0.2f);
Gizmos.DrawCube(transform.position + collider.center, collider.size);
}
}
using UnityEngine;
using UTJ;
[RequireComponent(typeof(SpringManager), typeof(Collider))]
public class WindReciever : MonoBehaviour
{
public bool isWindRecieve = true;
private SpringBone[] springBones;
WindBlower blower;
void Start()
{
springBones = GetComponent<SpringManager>().springBones;
}
private void OnTriggerEnter(Collider other)
{
if (!isWindRecieve) return;
blower = other.GetComponent<WindBlower>();
}
private void OnTriggerExit(Collider other)
{
blower = null;
RecieveWind(Vector3.zero);
}
private void OnTriggerStay(Collider other)
{
if (blower == null || !isWindRecieve)
{
RecieveWind(Vector3.zero);
return;
}
RecieveWind(blower.Force);
}
void RecieveWind(Vector3 recieveForce)
{
for (int i = 0; i < springBones.Length; i++)
{
springBones[i].springForce = recieveForce;
}
}
}
@kitposition
Copy link
Author

kitposition commented Apr 7, 2019

【概要】
ユニティちゃんについてくるRandomWind.csと同様の機能を持つが以下が異なる
・風力だけでなく風向の設定が可能
・風の影響範囲を設定してそのゾーン内に入ったキャラクターに影響が及ぶ
 (そのため送風機用とキャラクター用の2つのスクリプトで1組)
※送風機側・キャラクター側それぞれでON/OFFが可能なので、ゾーン内でキャラAのみ風に吹かれる、といったことも可能

【使い方】
●送風機側
・空のオブジェクトを用意し、WindBlower.csをアタッチ
・青いBoxColliderが付加されるので、サイズを調節。このCollider内が風が吹く範囲となる
・風力・向きはwindPowerに設定したVector3に従う(青い線で示される)
・isWindActiveで風のオン・オフを切り替え可能
●キャラクター側
・何らかのColliderをアタッチ
・WindReciever.csをアタッチ
・isWindRecieveで風の影響のオン・オフを切り替え可能

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment