十字キーのようなものでobjectを動かしたり回転させたりしたいのですが回転させた後に動かそうとすると意図していない方向に進んでしまって困っています。
自力で調べてみた結果ワールド座標とローカル座標がありtransform.Translate()はそのobjectの向きに対して動いてしまうためということまでわかり、下記のようにworldPosに変換して移動させてみたのですがすごい勢いでどこかへ移動していってしまいました。
Tlanslate()にlocalPosを渡したときのような動きを回転後も右へ動かすなら画面に対して右方向へといった感じの仕様にしたいのですがどのようにすればよいのでしょうか?
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StickController : MonoBehaviour { float speedX = 0; float speedZ = 0; GameObject Generator; // Use this for initialization void Start () { this.Generator = GameObject.Find("StickGenerator"); } // Update is called once per frame void Update () { Vector3 localPos = new Vector3(this.speedX, 0, this.speedZ); Vector3 worldPos = transform.TransformPoint(localPos); Generator.GetComponent<StickGenerator>().now.transform.Translate(worldPos ); this.speedX *= 0.96f; this.speedZ *= 0.96f; } //右へ動かす public void MoveRight() { this.speedX = 0.01f; } //左へ動かす public void MoveLeft() { this.speedX = -0.01f; } //手前へ動かす public void MoveFront() { this.speedZ = -0.01f; } //奥へ動かす public void MoveBack() { this.speedZ = 0.01f; } //右へ回転させる public void RotationRight() { Generator.GetComponent<StickGenerator>().now.transform.Rotate(new Vector3(45, 0, 0)); } //左へ回転させる public void RotationLeft() { Generator.GetComponent<StickGenerator>().now.transform.Rotate(new Vector3(-45, 0, 0)); } //手前へ回転させる public void RotationFront() { Generator.GetComponent<StickGenerator>().now.transform.Rotate(new Vector3(0, 0, -45)); } //奥へ回転させる public void RotationBack() { Generator.GetComponent<StickGenerator>().now. transform.Rotate(new Vector3(0, 0, 45)); } }
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/06 09:04