You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.2 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This is just a simple movement example in which the object is moved
  5. /// to a random location inside a bounding box using smooth lerp
  6. /// </summary>
  7. public class SimpleMoveExample : MonoBehaviour
  8. {
  9. private Vector3 m_previous;
  10. private Vector3 m_target;
  11. private Vector3 m_originalPosition;
  12. public Vector3 BoundingVolume = new Vector3( 3, 1, 3 );
  13. public float Speed = 10;
  14. private void Start()
  15. {
  16. m_originalPosition = transform.position;
  17. m_previous = transform.position;
  18. m_target = transform.position;
  19. }
  20. private void Update()
  21. {
  22. transform.position = Vector3.Slerp( m_previous, m_target, Time.deltaTime * Speed );
  23. m_previous = transform.position;
  24. if ( Vector3.Distance( m_target, transform.position ) < 0.1f )
  25. {
  26. m_target = transform.position + Random.onUnitSphere * Random.Range( 0.7f, 4f );
  27. m_target.Set( Mathf.Clamp( m_target.x, m_originalPosition.x - BoundingVolume.x, m_originalPosition.x + BoundingVolume.x ), Mathf.Clamp( m_target.y, m_originalPosition.y - BoundingVolume.y, m_originalPosition.y + BoundingVolume.y ), Mathf.Clamp( m_target.z, m_originalPosition.z - BoundingVolume.z, m_originalPosition.z + BoundingVolume.z ) );
  28. }
  29. }
  30. }