GoogleMap上でJoystickから3Dキャラを動かしたい
解決したいこと
GoogleMap上でJoystickから3Dキャラを動かしたいのですが
ジョイスティックからUnityちゃんが動くのですが、その場をくるくるするだけで
移動はしてくれません。解決方法を教えてください。
GoogleMapの参考ページ
https://dev.to/ikkou/maps-sdk-for-unity-4l65
こちらのサンプルシーン 04_Advancedで作業しております。
joystickはAssetsのJoystick PackからDynamic Joystickを使用しています。
Joystick実装の参考ページ
https://youdoyou-motto.com/apply_joystick_forcharacter?codoc_conversion=entry
発生している問題・エラー
エラーメッセージはありません。
該当するソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControlScript : MonoBehaviour
{
//移動処理に必要なコンポーネントを設定
public Animator animator; //モーションをコントロールするためAnimatorを取得
public CharacterController controller; //キャラクター移動を管理するためCharacterControllerを取得
//移動速度等のパラメータ用変数(inspectorビューで設定)
public float speed; //キャラクターの移動速度
public float jumpSpeed; //キャラクターのジャンプ力
public float rotateSpeed; //キャラクターの方向転換速度
public float gravity; //キャラにかかる重力の大きさ
Vector3 targetDirection; //移動する方向のベクトル
Vector3 moveDirection = Vector3.zero;
public Joystick joystick; //◆
// Start関数は変数を初期化するための関数
void Start()
{
}
// Update関数は1フレームに1回実行される
void Update()
{
moveControl(); //移動用関数
RotationControl(); //旋回用関数
//最終的な移動処理
//(これが無いとCharacterControllerに情報が送られないため、動けない)
controller.Move(moveDirection * Time.deltaTime);
}
void moveControl()
{
//★進行方向計算
//キーボード入力を取得
// float v = Input.GetAxisRaw("Vertical"); //InputManagerのWSまたは↑↓の入力
// float h = Input.GetAxisRaw("Horizontal"); //InputManagerのADまたは←→の入力
float v = joystick.Vertical; //◆ Joystickの入力を使用
float h = joystick.Horizontal; //◆ Joystickの入力を使用
//カメラの正面方向ベクトルからY成分を除き、正規化してキャラが走る方向を取得
Vector3 forward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
Vector3 right = Camera.main.transform.right; //カメラの右方向を取得
//カメラの方向を考慮したキャラの進行方向を計算
targetDirection = h * right + v * forward;
//★地上にいる場合の処理
if (controller.isGrounded)
{
//移動のベクトルを計算
moveDirection = targetDirection * speed;
//Jumpボタンでジャンプ処理
if (Input.GetButton("Jump")) //Spaceキーの入力
{
moveDirection.y = jumpSpeed;
}
}
else //空中操作の処理(重力加速度等)
{
float tempy = moveDirection.y;
//(↓の2文の処理があると空中でも入力方向に動けるようになる)
moveDirection = Vector3.Scale(targetDirection, new Vector3(1, 0, 1)).normalized; //◆ コメントアウト解除
moveDirection *= speed; //◆ コメントアウト解除
moveDirection.y = tempy - gravity * Time.deltaTime;
}
//★走行アニメーション管理
if (v > .1 || v < -.1 || h > .1 || h < -.1) //(移動入力があると)
{
animator.SetFloat("Speed", 1f); //キャラ走行のアニメーションON
}
else //(移動入力が無いと)
{
animator.SetFloat("Speed", 0f); //キャラ走行のアニメーションOFF
}
}
void RotationControl() //キャラクターが移動方向を変えるときの処理
{
Vector3 rotateDirection = moveDirection;
rotateDirection.y = 0;
//それなりに移動方向が変化する場合のみ移動方向を変える
if (rotateDirection.sqrMagnitude > 0.01)
{
//緩やかに移動方向を変える
float step = rotateSpeed * Time.deltaTime;
Vector3 newDir = Vector3.Slerp(transform.forward, rotateDirection, step);
transform.rotation = Quaternion.LookRotation(newDir);
}
}
public void Buttonjump() //ボタンによるジャンプ
{
moveDirection.y = jumpSpeed;
}
}
自分で試したこと
正直全くわからないです。
GoogleMap上だと動かないです。
キャラクターにコンポーネントしたスクリプトのinspectorの画像も掲載します。
他に必要な情報がありましたらご連絡ください。
よろしくお願いします。
0 likes