Code Review

1 using System;
2 using
UnityEngine;
3
4
5 namespace
UnityStandardAssets.Utility
6 {
7     
[Serializable]
8     
public class CurveControlledBob
9     {
10         
public float HorizontalBobRange = 0.33f;
11         
public float VerticalBobRange = 0.33f;
12         
public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f),
13                                                             
new Keyframe(1f, 0f), new Keyframe(1.5f, -1f),
14                                                             
new Keyframe(2f, 0f)); // sin curve for head bob
15         
public float VerticaltoHorizontalRatio = 1f;
16
17         
private float m_CyclePositionX;
18         
private float m_CyclePositionY;
19         
private float m_BobBaseInterval;
20         
private Vector3 m_OriginalCameraPosition;
21         
private float m_Time;
22
23
24         
public void Setup(Camera camera, float bobBaseInterval)
25         {
26             m_BobBaseInterval = bobBaseInterval;
27             m_OriginalCameraPosition = camera.transform.localPosition;
28
29             
// get the length of the curve in time
30             m_Time = Bobcurve[Bobcurve.length -
1].time;
31         }
32
33
34         
public Vector3 DoHeadBob(float speed)
35         {
36             
float xPos = m_OriginalCameraPosition.x + (Bobcurve.Evaluate(m_CyclePositionX)*HorizontalBobRange);
37             
float yPos = m_OriginalCameraPosition.y + (Bobcurve.Evaluate(m_CyclePositionY)*VerticalBobRange);
38
39             m_CyclePositionX += (speed*Time.deltaTime)/m_BobBaseInterval;
40             m_CyclePositionY += ((speed*Time.deltaTime)/m_BobBaseInterval)*VerticaltoHorizontalRatio;
41
42             
if (m_CyclePositionX > m_Time)
43             {
44                 m_CyclePositionX = m_CyclePositionX - m_Time;
45             }
46             
if (m_CyclePositionY > m_Time)
47             {
48                 m_CyclePositionY = m_CyclePositionY - m_Time;
49             }
50
51             
return new Vector3(xPos, yPos, 0f);
52         }
53     }
54 }


Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *