前提・実現したいこと
実現したいものに最も近いプログラムを誠に勝手ながら使用させて頂いております。
現状の動きとして、上下にドラッグするとオブジェクトが動きに合わせて傾くのですが、左右にドラッグするとカメラが動きます。これを、左右にドラッグしたら上下ドラッグと同じような挙動でオブジェクトを傾けさせたいのですが、如何せん、初心者なものでどのように改良すればよいかわからない状態にあります。
該当のソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DownController : MonoBehaviour
{
Rigidbody rigid;
// Start is called before the first frame update void Start() { rigid = GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { if (Input.GetMouseButton(0)) { OnRotate(new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"))); } } float RotationSpeed=1.0f; //deltaはドラッグの方向を取得しています。 void OnRotate(Vector2 delta) { // 回転量はドラッグ方向ベクトルの長さに比例する float deltaAngle = delta.magnitude * RotationSpeed; // 回転量がほぼ0なら、回転軸を求められないので何もしない if (Mathf.Approximately(deltaAngle, 0.0f)) { return; } // ドラッグ方向をワールド座標系に直す // 横ドラッグならカメラのright方向、縦ドラッグならup方向ということなので // deltaのx、yをright、upに掛けて、2つを合成すればいいはず... Transform cameraTransform = Camera.main.transform; Vector3 deltaWorld = cameraTransform.right * delta.x + cameraTransform.up * delta.y; // 回転軸はドラッグ方向とカメラforwardの外積の向き Vector3 axisWorld = Vector3.Cross(deltaWorld, cameraTransform.forward).normalized; // Rotateで回転する // 回転軸はワールド座標系に基づくので、回転もワールド座標系を使う transform.Rotate(axisWorld, deltaAngle, Space.World); }
C#
試したこと
一番下にあるtransform.Rotate(axisWorld,deltaAngle,Space.World) を transform.Rotate(deltaAngle,deltaAngle,Space.World) のように変更してみたのですが、そうすると右斜めに回転するようになりました
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー