前提・実現したいこと
unity3DでGetCompornent<Rigidbody>().velocityを用いてキャラクターの向いている方向の正面斜め上(0,1,1)にジャンプさせたいのですが、以下のソースコードでは何故か上方向にしか力がかからずその場ジャンプになってしまいます。
エラー等は発生せず実行は可能です。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Jumper : MonoBehaviour 6{ 7 8 Vector3 force; 9 new Rigidbody rigidbody; 10 11 void OnTriggerExit(Collider other) 12 { 13 14 Player.animator.SetBool("Walking", false); 15 Player.animator.SetBool("Running", false); 16 Player.animator.SetBool("Jumping", true); 17 StartCoroutine("Jump"); 18 } 19 20 private IEnumerator Jump() 21 { 22 rigidbody = MazeStageGeneral.utc.GetComponent<Rigidbody>(); 23 MazeStageGeneral.utc.GetComponent<Player>().enabled = false; 24 MazeStageGeneral.utc.transform.localEulerAngles = transform.localEulerAngles; 25 force =(Vector3.up + Vector3.forward) * 3; 26 27 yield return new WaitForSeconds(0.4f); 28 29 GetComponent<ParticleSystem>().Play(); 30 rigidbody.velocity = force; 31 32 33 yield return new WaitForSeconds(1f); 34 Player.animator.SetBool("Jumping", false); 35 MazeStageGeneral.utc.GetComponent<Player>().enabled = true; 36 } 37} 38
Player.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player: MonoBehaviour 6{ 7 [SerializeField] Vector3 velocity; //移動方向 8 public static float speed = 2.0f; //移動速度 9 [SerializeField] float rotation = 0.2f; //回転速度 10 public static Animator animator; 11 12 void Start() 13 { 14 animator = GetComponent<Animator>(); 15 } 16 17 void Update() 18 { 19 velocity = Vector3.zero; 20 21 if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.UpArrow)) 22 { 23 animator.SetBool("Walking", true); 24 if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); } 25 26 if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 27 { 28 speed = 5.0f; 29 if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); } 30 animator.SetBool("Walking", false); 31 animator.SetBool("Running", true); 32 } 33 if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift)) 34 { 35 speed = 2.0f; 36 if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); } 37 animator.SetBool("Running", false); 38 animator.SetBool("Walking", true); 39 } 40 } 41 else 42 { 43 animator.SetBool("Walking", false); 44 animator.SetBool("Running", false); 45 if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); } 46 } 47 48 if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) 49 { 50 //移動(X軸、Y軸、Z軸) 51 velocity.x += 1; 52 } 53 if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) 54 { 55 velocity.x -= 1; 56 } 57 if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) 58 { 59 velocity.z += 1; 60 } 61 if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) 62 { 63 velocity.z -= 1; 64 } 65 if ((Input.GetKey(KeyCode.W)&& Input.GetKey(KeyCode.A)) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow))) 66 { 67 velocity.x -= 1; 68 velocity.z += 1; 69 } 70 if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))) 71 { 72 velocity.x += 1; 73 velocity.z += 1; 74 } 75 if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))) 76 { 77 velocity.x -= 1; 78 velocity.z -= 1; 79 } 80 if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))) 81 { 82 velocity.x += 1; 83 velocity.z -= 1; 84 } 85 86 velocity = velocity.normalized * speed * Time.deltaTime; 87 if(velocity.magnitude > 0) 88 { 89 transform.position += velocity; 90 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), rotation); 91 } 92 93 } 94 95} 96
試したこと
AddForceなども試しましたが挙動が怪しく、velocityで実装したい所存です。
補足情報(FW/ツールのバージョンなど)
Unity 2017.3.1f1 Personal(64bit)
Microsoft Visual Studio 2016
windows10