Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【Unity】 ベルトコンベアを作ってみる
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BeltConveyor : MonoBehaviour
{
[SerializeField]
private float m_uvSpeed = 1.0f;
[SerializeField]
private float m_movePower = 100.0f;
[SerializeField]
private float m_speedUpPower = 100.0f;
[SerializeField]
private float m_speedUpTime = 3.0f;
private Renderer m_render = null;
private List<Rigidbody> m_hitObjects = new List<Rigidbody>();
void Awake()
{
m_render = GetComponent< Renderer >();
}
void Start()
{
StartCoroutine( SpeedUp( m_speedUpTime ) );
}
void Update()
{
ScrollUV();
}
void OnCollisionEnter( Collision other )
{
var body = other.gameObject.GetComponent<Rigidbody>();
if( body != null )
{
Vector3 addPower = transform.forward * m_movePower;
body.AddForce( addPower, ForceMode.Acceleration );
m_hitObjects.Add( body );
}
}
void OnCollisionExit( Collision other )
{
var body = other.gameObject.GetComponent<Rigidbody>();
if( body != null )
{
Vector3 addPower = transform.forward * m_movePower;
body.AddForce( -addPower, ForceMode.Acceleration );
m_hitObjects.Remove( body );
}
}
/// <summary>
/// テクスチャのUV値をスクロールさせて、ベルトコンベアの見た目を表現する
/// </summary>
void ScrollUV()
{
var material = m_render.material;
Vector2 offset = material.mainTextureOffset;
offset += Vector2.up * m_uvSpeed * Time.deltaTime;
material.mainTextureOffset = offset;
}
IEnumerator SpeedUp( float i_time )
{
while( true )
{
// 一定時間ごとにスピードアップ
yield return new WaitForSeconds( i_time );
m_movePower += m_speedUpPower;
// 現在乗っているオブジェクトに対してスピードアップ分力を加える
Vector3 addPower = transform.forward * m_speedUpPower;
foreach( var body in m_hitObjects )
{
if( body != null )
{
body.AddForce( addPower, ForceMode.Acceleration );
}
}
}
}
} // class BeltConveyor
using UnityEngine;
using System.Collections;
public class BeltObjectGenerator : MonoBehaviour
{
[SerializeField]
private GameObject m_object = null;
[SerializeField]
private float m_time = 2.0f;
void Start()
{
StartCoroutine( Generator( m_time ) );
}
private IEnumerator Generator( float i_time )
{
var waitTime = new WaitForSeconds( i_time );
while( true )
{
yield return waitTime;
Vector3 offset = Vector3.zero;
offset.x = Random.Range( -4.0f, 4.0f );
var obj = GameObject.Instantiate( m_object, transform.position + offset, Quaternion.identity ) as GameObject;
obj.SetActive( true );
}
}
} // class BeltObjectGenerator
using UnityEngine;
public class BeltPlayer : MonoBehaviour
{
// 移動に与える力
[SerializeField]
private float m_movePower = 500.0f;
// 前回与えた移動の力
private Vector3 m_prevVelocity = Vector3.zero;
void Update()
{
var body = GetComponent<Rigidbody>();
// 前回与えた力の逆方向の力を与えて相殺
body.AddForce( -m_prevVelocity );
var velocity = Vector3.zero;
if( Input.GetKey( KeyCode.UpArrow ) )
{
velocity += Vector3.forward;
}
if( Input.GetKey( KeyCode.DownArrow ) )
{
velocity += Vector3.back;
}
if( Input.GetKey( KeyCode.LeftArrow ) )
{
velocity += Vector3.left;
}
if( Input.GetKey( KeyCode.RightArrow ) )
{
velocity += Vector3.right;
}
velocity *= m_movePower;
body.AddForce( velocity );
// 与えた力を保存
m_prevVelocity = velocity;
}
} // class BeltPlayer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.