Created
September 30, 2017 14:24
-
-
Save urahimono/bd503a99a0ddbae727a3f50f782bb7e9 to your computer and use it in GitHub Desktop.
【Unity】離れたら触れるようになるオブジェクト
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
//------------------------------------------------------------------------ | |
// | |
// (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 |
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
//------------------------------------------------------------------------ | |
// | |
// (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 |
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
//------------------------------------------------------------------------ | |
// | |
// (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