wangguan
2020-12-29 452c75675679c44cc39b04bdb7d330d7c5c14d5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace MoreMountains.NiceVibrations
{
    public class BallDemoBall : MonoBehaviour
    {
        public bool HapticsEnabled = true;
        public ParticleSystem HitParticles;
        public ParticleSystem HitPusherParticles;
        public LayerMask WallMask;
        public LayerMask PusherMask;
        public MMUIShaker LogoShaker;
        public AudioSource TransientAudioSource;
 
        protected Rigidbody2D _rigidBody;
        protected float _lastRaycastTimestamp = 0f;
        protected Animator _ballAnimator;
        protected int _hitAnimationParameter;
 
        protected virtual void Awake()
        {
            _rigidBody = this.gameObject.GetComponent<Rigidbody2D>();
            _ballAnimator = this.gameObject.GetComponent<Animator>();
            _hitAnimationParameter = Animator.StringToHash("Hit");
            MMNViOSCoreHaptics.CreateEngine();
        }
 
        protected virtual void OnCollisionEnter2D(Collision2D collision)
        {
            if (WallMask == (WallMask | (1 << collision.gameObject.layer)))
            {
                HitWall();
            }
        }
 
        protected virtual void Update()
        {
            float raycastLength = 5f;
 
            Debug.DrawLine(this.transform.position, Vector3.down * raycastLength, Color.red);
 
            if (Time.time - _lastRaycastTimestamp > 1f)
            {
                _lastRaycastTimestamp = Time.time;
                RaycastHit2D hit = Physics2D.Raycast(this.transform.position, Vector2.down, raycastLength, WallMask);
                if (hit.collider != null)
                {
                    HitBottom();
                }
            }
        }
 
        protected virtual void HitBottom()
        {
            _rigidBody.AddForce(Vector2.up * 2500f);
            StartCoroutine(LogoShaker.Shake(0.2f));
        }
 
        protected virtual void HitWall()
        {
            float intensity = _rigidBody.velocity.magnitude / 100f;
            MMVibrationManager.TransientHaptic(intensity, 0.7f, true, this);
            TransientAudioSource.volume = intensity;
            StartCoroutine(LogoShaker.Shake(0.2f));
            TransientAudioSource.Play();
            _ballAnimator.SetTrigger(_hitAnimationParameter);
        }
 
        public virtual void HitPusher()
        {
            HitPusherParticles.Play();
            MMVibrationManager.TransientHaptic(0.85f, 0.05f, true, this);
            TransientAudioSource.volume = 0.1f;
            StartCoroutine(LogoShaker.Shake(0.2f));
            TransientAudioSource.Play();
            _ballAnimator.SetTrigger(_hitAnimationParameter);
        }
    }
}