ざっくりまとめるとタイトルに含まれているゲームのように、通常ゲームでは下に向かって重力が働いていますが
今回は惑星に対して重力を働かせてその重力に従いキャラを歩かせようと考えています。
重力の向きを変更することには成功しましたが、キャラの足が常に地面に向かうようにベクトルを操作することが出来ません。
やったこと
1キャラクターをLookAtで惑星に常に向かせる
→キャラの3Dモデルの向きがy軸方向に設定されており、つまりは体全体が惑星に向かって向いていてよこだおしになっている状態
2LookAtを消しキャラクターの向きベクトルを取得、そののち向きベクトルとキャラクターにかかる重力ベクトルの内積を0にさせる
→キャラを立たせることが出来たが実際に動かした時惑星ではなく、Unityの通常の重力に従って動きなおかつキャラの足先が惑星の方へ向かなくなったため失敗
結論 やりたいこと
キャラクターが惑星の重力に従ってスムーズに動いているように見せたい
下記はキャラクターについているスクリプトです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BearAnimConScp : MonoBehaviour 6{ 7 private static float G= 6.67259f; 8 Rigidbody br_body, sp_body;//キャラと惑星のRigidBoody 9 public GameObject Sphere;//惑星 10 Vector3 dir, heading,go_dir; 11 float speed = 5.0f; 12 float angleDir,x_dir; 13 // Start is called before the first frame update 14 void Start() 15 { 16 sp_body = GetComponent<Rigidbody>(); 17 br_body= GetComponent<Rigidbody>(); 18 19 x_dir = go_dir.x * heading.x+go_dir.y*heading.y+go_dir.z*heading.z;//内積0を求めている 20 } 21 22 // Update is called once per frame 23 void Update() 24 { 25 angleDir = transform.eulerAngles.z * (Mathf.PI / 180.0f); 26 go_dir = new Vector3(Mathf.Cos(angleDir), Mathf.Sin(angleDir), 0.0f);//キャラクターの向きベクトル 27 28 heading = (Sphere.transform.position - this.transform.position) / 1; 29 dir = G * heading.normalized * (sp_body.mass * br_body.mass) / (heading.sqrMagnitude);//重力計算 30 31 br_body.AddForce(dir);//重力実行 32 33 if (Input.GetKey("up")) 34 { 35 transform.position += transform.up * speed * Time.deltaTime; 36 } 37 if (Input.GetKey("down")) 38 { 39 transform.position -= transform.up * speed * Time.deltaTime; 40 } 41 if (Input.GetKey("right")) 42 { 43 transform.position += transform.right * speed * Time.deltaTime; 44 } 45 if (Input.GetKey("left")) 46 { 47 transform.position -= transform.right * speed * Time.deltaTime; 48 } 49 } 50} 51
どうかよろしくお願いいたします。
編集
惑星の上をキャラクターが歩くことまではできました。
ただ右左の矢印キーを押すとカメラが一瞬おかしくなってしまうことが悩みです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BearAnimConScp : MonoBehaviour 6{ 7 private static float G= 6.67259f; 8 Rigidbody br_body, sp_body; 9 public GameObject Sphere; 10 Vector3 dir, heading,go_dir; 11 float speed = 15.0f; 12 float angleDir,x_dir; 13 private Vector3 move; 14 private Vector3 mir_move; 15 // Start is called before the first frame update 16 void Start() 17 { 18 sp_body = GetComponent<Rigidbody>(); 19 br_body= GetComponent<Rigidbody>(); 20 move = new Vector3(0,0,0); 21 mir_move = -move; 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 if (Input.GetKey("up")) 28 { 29 transform.position += transform.forward * speed * Time.deltaTime; 30 move+= transform.forward * speed * Time.deltaTime; 31 transform.rotation = Quaternion.LookRotation(-dir, -move) * Quaternion.FromToRotation(Vector3.up, Vector3.forward); 32 } 33 if (Input.GetKey("down")) 34 { 35 transform.position -= transform.forward * speed * Time.deltaTime; 36 move-= transform.forward * speed * Time.deltaTime; 37 transform.rotation = Quaternion.LookRotation(-dir, -this.transform.forward) * Quaternion.FromToRotation(Vector3.up, Vector3.forward); 38 } 39 if (Input.GetKey("right")) 40 { 41 transform.position += transform.right * speed * Time.deltaTime; 42 move+= transform.right * speed * Time.deltaTime; 43 transform.rotation = Quaternion.LookRotation(this.transformm.forward, -dir) * Quaternion.FromToRotation(Vector3.up, Vector3.right); 44 } 45 if (Input.GetKey("left")) 46 { 47 transform.position -= transform.right * speed * Time.deltaTime; 48 move-= transform.right * speed * Time.deltaTime; 49 transform.rotation = Quaternion.LookRotation(this.transform.forward,-dir) * Quaternion.FromToRotation(Vector3.up, Vector3.left); 50 } 51 52 if (!Input.anyKeyDown) 53 { 54 move = new Vector3(0, 0, 0); 55 transform.rotation = Quaternion.LookRotation(-dir, -this.transform.forward) * Quaternion.FromToRotation(Vector3.up, Vector3.forward); 56 } 57 58 heading = (Sphere.transform.position - this.transform.position) / 1; 59 dir = G * heading.normalized * (sp_body.mass * br_body.mass) / (heading.sqrMagnitude); 60 br_body.AddForce(dir*20); 61 } 62} 63
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/11 10:18