前提
unityでFPS作ってます。
発生している問題
キャラクター移動時のカメラの震え
実際はキャラクターの移動とカメラ操作を同時にすると写っているオブジェクトが細かに震えます。
移動
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.InputSystem; 5 6public class playermove : MonoBehaviour 7{ 8 Rigidbody rb; 9 public float speed = 5f; 10 public float dashspeed = 10f; 11 12 public float jumpforce; 13 public float jumpcooldown; 14 public float airmultiplier; 15 bool readytojump; 16 17 Vector2 axis; 18 Vector3 movedirection; 19 public Transform orientation; 20 21 public float grounddrag; 22 23 [Header("ground check")] 24 public float playerheight; 25 public LayerMask whatIsground; 26 bool grounded; 27 28 bool checktest; 29 30 31 32 33 private void Awake() 34 { 35 rb = GetComponent<Rigidbody>(); 36 rb.freezeRotation = true; 37 var gravity = rb.useGravity; 38 } 39 40 41 public void move(InputAction.CallbackContext context) 42 { 43 axis = context.ReadValue<Vector2>(); 44 //velocity = new Vector3(axis.x, 0,axis.y); 45 46 47 } 48 49 public void jump(InputAction.CallbackContext context) 50 { 51 52 checktest = context.action.triggered; 53 if (checktest && grounded) 54 { 55 //readytojump = false; 56 57 jump(); 58 59 Invoke(nameof(resetjump), jumpcooldown); 60 } 61 62 63 } 64 65 66 // Start is called before the first frame update 67 void Start() 68 { 69 70 } 71 72 private void Update() 73 { 74 // ground check 75 grounded = Physics.Raycast(transform.position, Vector3.down, playerheight * 0.5f + 0.2f); 76 77 // handle drag 78 if (grounded) rb.drag = grounddrag; 79 else rb.drag = 0f; 80 81 speedcontrol(); 82 } 83 84 // Update is called once per frame 85 void FixedUpdate() 86 { 87 walk(); 88 89 90 } 91 92 void walk() 93 { 94 movedirection = orientation.forward * axis.y + orientation.right * axis.x; 95 if(grounded) rb.AddForce(movedirection.normalized * speed * 10f, ForceMode.Force); 96 else if(!grounded) rb.AddForce(movedirection.normalized * speed * 10f * airmultiplier, ForceMode.Force); 97 98 } 99 100 void speedcontrol() 101 { 102 Vector3 flatvel = new Vector3(rb.velocity.x, 0f, rb.velocity.z); 103 104 if (flatvel.magnitude > speed) 105 { 106 Vector3 limitedvel = flatvel.normalized * speed; 107 rb.velocity = new Vector3(limitedvel.x, rb.velocity.y, limitedvel.z); 108 } 109 } 110 111 void jump() 112 { 113 rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z); 114 rb.AddForce(transform.up * jumpforce, ForceMode.Impulse); 115 116 } 117 118 void resetjump() 119 { 120 readytojump = true; 121 122 } 123} 124
マウス操作
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class mouselook : MonoBehaviour 6{ 7 public float sensX; 8 public float sensY; 9 10 //public Transform orientation; 11 public Transform playerbody; 12 13 float xRotation; 14 float yRotation; 15 16 private void Start() 17 { 18 Cursor.lockState = CursorLockMode.Locked; 19 Cursor.visible = false; 20 21 22 } 23 24 private void Update() 25 { 26 27 float mouseX = Input.GetAxis("Mouse X") * sensX * Time.deltaTime; 28 float mouseY = Input.GetAxis("Mouse Y") * sensY * Time.deltaTime; 29 30 yRotation += mouseX; 31 xRotation -= mouseY; 32 33 xRotation = Mathf.Clamp(xRotation, -90f, 90f); 34 35 playerbody.Rotate(Vector3.up * mouseX); 36 transform.rotation = Quaternion.Euler(xRotation, yRotation, 0); 37 38 39 } 40 41 42} 43
写真のように白い箱に焦点を当てながら、横移動をすると箱がというかカメラがぷるぷる震えます。
移動だけさせただけだとカメラは震えません。マウスを動かしたときのみカメラが震えていると思われます。
ゲームビューは写真のように銃を構えさせていますが、シーンビューでマウスだけを動かすと銃がカクカクッと動くのが確認できました。なんかコマ割りみたいな?銃の位置が瞬間移動するみたいな?要するに全然ヌルヌルじゃねぇじゃねぇか。ということです。ほんとヌルヌルしてほしいですよ。
このカクカクが原因なのでしょうか。
あなたの回答
tips
プレビュー