ざっくりまとめるとタイトルに含まれているゲームのように、通常ゲームでは下に向かって重力が働いていますが
今回は惑星に対して重力を働かせてその重力に従いキャラを歩かせようと考えています。
重力の向きを変更することには成功しましたが、キャラの足が常に地面に向かうようにベクトルを操作することが出来ません。
やったこと
1キャラクターをLookAtで惑星に常に向かせる
→キャラの3Dモデルの向きがy軸方向に設定されており、つまりは体全体が惑星に向かって向いていてよこだおしになっている状態
2LookAtを消しキャラクターの向きベクトルを取得、そののち向きベクトルとキャラクターにかかる重力ベクトルの内積を0にさせる
→キャラを立たせることが出来たが実際に動かした時惑星ではなく、Unityの通常の重力に従って動きなおかつキャラの足先が惑星の方へ向かなくなったため失敗
結論 やりたいこと
キャラクターが惑星の重力に従ってスムーズに動いているように見せたい
下記はキャラクターについているスクリプトです。
C#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BearAnimConScp : MonoBehaviour { private static float G= 6.67259f; Rigidbody br_body, sp_body;//キャラと惑星のRigidBoody public GameObject Sphere;//惑星 Vector3 dir, heading,go_dir; float speed = 5.0f; float angleDir,x_dir; // Start is called before the first frame update void Start() { sp_body = GetComponent<Rigidbody>(); br_body= GetComponent<Rigidbody>(); x_dir = go_dir.x * heading.x+go_dir.y*heading.y+go_dir.z*heading.z;//内積0を求めている } // Update is called once per frame void Update() { angleDir = transform.eulerAngles.z * (Mathf.PI / 180.0f); go_dir = new Vector3(Mathf.Cos(angleDir), Mathf.Sin(angleDir), 0.0f);//キャラクターの向きベクトル heading = (Sphere.transform.position - this.transform.position) / 1; dir = G * heading.normalized * (sp_body.mass * br_body.mass) / (heading.sqrMagnitude);//重力計算 br_body.AddForce(dir);//重力実行 if (Input.GetKey("up")) { transform.position += transform.up * speed * Time.deltaTime; } if (Input.GetKey("down")) { transform.position -= transform.up * speed * Time.deltaTime; } if (Input.GetKey("right")) { transform.position += transform.right * speed * Time.deltaTime; } if (Input.GetKey("left")) { transform.position -= transform.right * speed * Time.deltaTime; } } }
どうかよろしくお願いいたします。
編集
惑星の上をキャラクターが歩くことまではできました。
ただ右左の矢印キーを押すとカメラが一瞬おかしくなってしまうことが悩みです。
C#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BearAnimConScp : MonoBehaviour { private static float G= 6.67259f; Rigidbody br_body, sp_body; public GameObject Sphere; Vector3 dir, heading,go_dir; float speed = 15.0f; float angleDir,x_dir; private Vector3 move; private Vector3 mir_move; // Start is called before the first frame update void Start() { sp_body = GetComponent<Rigidbody>(); br_body= GetComponent<Rigidbody>(); move = new Vector3(0,0,0); mir_move = -move; } // Update is called once per frame void Update() { if (Input.GetKey("up")) { transform.position += transform.forward * speed * Time.deltaTime; move+= transform.forward * speed * Time.deltaTime; transform.rotation = Quaternion.LookRotation(-dir, -move) * Quaternion.FromToRotation(Vector3.up, Vector3.forward); } if (Input.GetKey("down")) { transform.position -= transform.forward * speed * Time.deltaTime; move-= transform.forward * speed * Time.deltaTime; transform.rotation = Quaternion.LookRotation(-dir, -this.transform.forward) * Quaternion.FromToRotation(Vector3.up, Vector3.forward); } if (Input.GetKey("right")) { transform.position += transform.right * speed * Time.deltaTime; move+= transform.right * speed * Time.deltaTime; transform.rotation = Quaternion.LookRotation(this.transformm.forward, -dir) * Quaternion.FromToRotation(Vector3.up, Vector3.right); } if (Input.GetKey("left")) { transform.position -= transform.right * speed * Time.deltaTime; move-= transform.right * speed * Time.deltaTime; transform.rotation = Quaternion.LookRotation(this.transform.forward,-dir) * Quaternion.FromToRotation(Vector3.up, Vector3.left); } if (!Input.anyKeyDown) { move = new Vector3(0, 0, 0); transform.rotation = Quaternion.LookRotation(-dir, -this.transform.forward) * Quaternion.FromToRotation(Vector3.up, Vector3.forward); } heading = (Sphere.transform.position - this.transform.position) / 1; dir = G * heading.normalized * (sp_body.mass * br_body.mass) / (heading.sqrMagnitude); br_body.AddForce(dir*20); } }
まだ回答がついていません
会員登録して回答してみよう