unity初心者です
WASDキーのどれかを押すカメラが大きく揺れて何も押してない状態だと小さく揺れるスクリプトを書いたんですが、小さい揺れから大きい揺れになる間をいい感じ調節することができません。
CurveControlledBob.csが動作を調整するスクリプト、Move.csが実行するスクリプトです。
CurveControlledBob.cs
1using System; 2using UnityEngine; 3 4[Serializable] 5public class CurveControlledBob 6{ 7 public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f), 8 new Keyframe(1f, 0f), new Keyframe(1.5f, -1f), 9 new Keyframe(2f, 0f)); // sin curve for head bob 10 public float VerticaltoHorizontalRatio; 11 12 private float m_CyclePositionX; 13 private float m_CyclePositionY; 14 private float m_BobBaseInterval; 15 private Vector3 m_OriginalCameraPosition; 16 private float m_Time; 17 private float kWalkHorizontalBobRange = 0.05f; 18 private float kWalkVerticalBobRange = 0.05f; 19 private float kStopHorizontalBobRange = 0.05f; 20 private float kStopVerticalBobRange = 0.05f; 21 private float horizontalBobRange = 0.02f; 22 private float verticalBobRange = 0.02f; 23 private float kChangeBobSpeed = 0.04f; 24 25 26 public void Setup(Camera camera, float bobBaseInterval) 27 { 28 m_BobBaseInterval = bobBaseInterval; 29 m_OriginalCameraPosition = camera.transform.localPosition; 30 31 // get the length of the curve in time 32 m_Time = Bobcurve[Bobcurve.length - 1].time; 33 } 34 35 36 public Vector3 DoHeadBob(float speed, bool isWalk) 37 { 38 if (isWalk) 39 { 40 if (horizontalBobRange < kWalkHorizontalBobRange) 41 { 42 horizontalBobRange += kChangeBobSpeed; 43 } 44 if (verticalBobRange < kWalkVerticalBobRange) 45 { 46 verticalBobRange += kChangeBobSpeed; 47 } 48 } 49 else 50 { 51 if (horizontalBobRange > kStopHorizontalBobRange) 52 { 53 horizontalBobRange -= kChangeBobSpeed; 54 } 55 if (verticalBobRange > kStopVerticalBobRange) 56 { 57 verticalBobRange -= kChangeBobSpeed; 58 } 59 } 60 61 float xPos = m_OriginalCameraPosition.x + (Bobcurve.Evaluate(m_CyclePositionX) * horizontalBobRange); 62 float yPos = m_OriginalCameraPosition.y + (Bobcurve.Evaluate(m_CyclePositionY) * verticalBobRange); 63 64 m_CyclePositionX += (speed * Time.deltaTime) / m_BobBaseInterval; 65 m_CyclePositionY += ((speed * Time.deltaTime) / m_BobBaseInterval) * VerticaltoHorizontalRatio; 66 67 if (m_CyclePositionX > m_Time) 68 { 69 m_CyclePositionX = m_CyclePositionX - m_Time; 70 } 71 if (m_CyclePositionY > m_Time) 72 { 73 m_CyclePositionY = m_CyclePositionY - m_Time; 74 } 75 76 return new Vector3(xPos, yPos, 0f); 77 } 78}
Move.cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Move : MonoBehaviour 6{ 7 [SerializeField] private CurveControlledBob headBob_ = new CurveControlledBob(); 8 9 private Camera mainCamera_; 10 private Transform cameraTransform_; 11 // Start is called before the first frame update 12 void Start() 13 { 14 mainCamera_ = this.GetComponent<Camera>(); 15 headBob_.Setup(mainCamera_, 1.0f); 16 cameraTransform_ = GetComponent<Transform>(); 17 } 18 19 // Update is called once per frame 20 void Update() 21 { 22 if (Input.GetKey(KeyCode.W) 23 || Input.GetKey(KeyCode.A) 24 || Input.GetKey(KeyCode.S) 25 || Input.GetKey(KeyCode.D)) 26 { 27 Vector3 handBob = headBob_.DoHeadBob(0.9f,true); 28 cameraTransform_.localPosition = handBob; 29 } 30 else 31 { 32 Vector3 handBob = headBob_.DoHeadBob(0.4f, false); 33 cameraTransform_.localPosition = handBob; 34 } 35 36 } 37}
回答1件
あなたの回答
tips
プレビュー