Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【Unity】離れたら触れるようになるオブジェクト
//------------------------------------------------------------------------
//
// (C) Copyright 2017 Urahimono Project Inc.
//
//------------------------------------------------------------------------
using UnityEngine;
public class ExitBoxTrigger : MonoBehaviour
{
public event System.Action onExit = () => {};
private void OnTriggerExit( Collider i_other )
{
onExit();
}
public void Initialize( Transform i_parent, BoxCollider i_sourceCollider, int i_triggerLayer )
{
transform.SetParent( i_parent, false );
var collider = gameObject.AddComponent<BoxCollider>();
collider.isTrigger = true;
collider.center = i_sourceCollider.center;
collider.size = i_sourceCollider.size;
gameObject.layer = i_triggerLayer;
}
} // class ExitTrigger
//------------------------------------------------------------------------
//
// (C) Copyright 2016 Urahimono Project Inc.
//
//------------------------------------------------------------------------
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif // UNITY_EDITOR
public class LayerTypeFieldAttribute : PropertyAttribute
{
} // class TagAttribute
#if UNITY_EDITOR
[CustomPropertyDrawer( typeof( LayerTypeFieldAttribute ) )]
public class LayerTypeFieldDrawer : PropertyDrawer
{
public override void OnGUI( Rect i_position, SerializedProperty i_property, GUIContent i_label )
{
i_property.intValue = EditorGUI.LayerField( i_position, i_label, i_property.intValue );
}
public override float GetPropertyHeight( SerializedProperty i_property, GUIContent i_label )
{
return EditorGUI.GetPropertyHeight( i_property );
}
} // class TagAttribute
#endif // UNITY_EDITOR
//------------------------------------------------------------------------
//
// (C) Copyright 2017 Urahimono Project Inc.
//
//------------------------------------------------------------------------
using UnityEngine;
[RequireComponent( typeof( Rigidbody ) )]
[RequireComponent( typeof( BoxCollider ) )]
public class ObjectController : MonoBehaviour
{
[Header( "Control" )]
[SerializeField]
private bool m_controlled = true;
[SerializeField]
private float m_moveSpeed = 0.0f;
[SerializeField]
private float m_turnSpeed = 0.0f;
[SerializeField]
private float m_jumpForce = 0.0f;
[Header( "Layer" )]
[SerializeField, LayerTypeField]
private int m_copiedLayer = 0;
[SerializeField, LayerTypeField]
private int m_defaultLayer = 0;
[SerializeField, LayerTypeField]
private int m_exitLayer = 0;
private ExitBoxTrigger m_exitTrigger = null;
private Rigidbody m_rigidbody = null;
private Renderer m_renderer = null;
private Color m_defaultColor = Color.white;
private Color ObjectColor
{
get { return m_renderer.material.color; }
set { m_renderer.material.color = value; }
}
private void Awake()
{
m_rigidbody = GetComponent<Rigidbody>();
m_renderer = GetComponentInChildren<Renderer>();
m_defaultColor = ObjectColor;
}
private void Update()
{
if( m_controlled )
{
ControlObject();
}
}
private void ApplyDefaultObject()
{
gameObject.layer = m_defaultLayer;
ObjectColor = m_defaultColor;
}
private void ControlObject()
{
Vector3 moveDir = Vector3.zero;
if( Input.GetKey( KeyCode.UpArrow ) )
{
moveDir += Vector3.forward;
}
if( Input.GetKey( KeyCode.DownArrow ) )
{
moveDir += Vector3.back;
}
if( Input.GetKey( KeyCode.RightArrow ) )
{
moveDir += Vector3.right;
}
if( Input.GetKey( KeyCode.LeftArrow ) )
{
moveDir += Vector3.left;
}
if( moveDir.sqrMagnitude > Mathf.Epsilon )
{
moveDir = moveDir.normalized;
Turn( moveDir );
Move( moveDir );
}
if( Input.GetKeyDown( KeyCode.Space ) )
{
Jump();
}
if( Input.GetKeyDown( KeyCode.A ) )
{
Duplication();
}
}
private void Move( Vector3 i_forward )
{
Vector3 delta = i_forward * m_moveSpeed * Time.deltaTime;
Vector3 targetPos = transform.position + delta;
m_rigidbody.MovePosition( targetPos );
}
private void Turn( Vector3 i_forward )
{
Quaternion toRot = Quaternion.LookRotation( i_forward );
Quaternion fromRot = transform.rotation;
float delta = m_turnSpeed * Time.deltaTime;
Quaternion targetRot = Quaternion.RotateTowards( fromRot, toRot, delta );
m_rigidbody.MoveRotation( targetRot );
}
private void Jump()
{
// えっ、このままじゃ空中でもジャンプできちゃうって!?
// 仕様だよ!
m_rigidbody.velocity = Vector3.zero;
Vector3 jumpVec = Vector3.up * m_jumpForce;
m_rigidbody.AddForce( jumpVec, ForceMode.Impulse );
}
private void Duplication()
{
var copiedObj = Instantiate( this, transform.position, transform.rotation );
copiedObj.SetDuplicationParameter();
}
private void SetDuplicationParameter()
{
m_controlled = false;
gameObject.layer = m_copiedLayer;
ObjectColor = new Color( 0.0f, 0.0f, 0.0f, 0.3f );
var trigger = new GameObject( "Trigger" ).AddComponent<ExitBoxTrigger>();
trigger.Initialize( transform, GetComponent<BoxCollider>(), m_exitLayer );
trigger.onExit += OnExitObject;
m_exitTrigger = trigger;
}
private void OnExitObject()
{
ApplyDefaultObject();
if( m_exitTrigger != null )
{
Destroy( m_exitTrigger.gameObject );
}
}
} // class ObjectController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment