//tips
//キャラクターを滑らかに動かす
カメラを追従させた場合のキャラクターの動きのカクツキが気になるので、キャラクターの動きを改善していく。
下記サイトを参考にスクリプトを作成した。
https://gametukurikata.com/program/rigidbodyandcollider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Envplayermove : MonoBehaviour
{
private Vector3 velocity;
// 入力値
private Vector3 input;
// 歩く速さ
[SerializeField]
private float walkSpeed = 1.5f;
// rigidbody
private Rigidbody rigid;
void Start()
{
rigid = GetComponent<Rigidbody>();
}
void Update()//移動の速度計算
{
input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
// 方向キーが多少押されている
if (input.magnitude > 0f)
{
transform.LookAt(transform.position + input);
velocity += transform.forward * walkSpeed;
}
}
void FixedUpdate()//実際の移動
{
// キャラクターを移動させる処理
rigid.MovePosition(transform.position + velocity * Time.fixedDeltaTime);
}
}
動きは滑らかになったが、カメラの切り替えはやはり急で、キーボードの矢印キーでの移動方向は初期時点から固定されている。プレイヤーの正面から右に移動としないとわかりにくい。
スクリプトを下記に組み替えたが、
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Envplayermove2 : MonoBehaviour
{
float inputHorizontal;
float inputVertical;
float mixvalue;
float Lmixvalue;
Rigidbody rb;
float moveSpeed = 1f;
Vector3 lastmov_f;
void Start()
{
rb = GetComponent<Rigidbody>();
lastmov_f = transform.position;
Lmixvalue = 0;
}
void Update()
{
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
mixvalue = inputHorizontal + inputVertical;
if(Lmixvalue != mixvalue)
{
MoveGo();
Lmixvalue = mixvalue;
Debug.Log("aaa");
}
}
void MoveGo()
{
Vector3 cameraForward = transform.position - lastmov_f;
// 方向キーの入力値とカメラの向きから、移動方向を決定
//Vector3 moveForward = cameraForward.normalized * inputVertical + Camera.main.transform.right * inputHorizontal;
Vector3 moveForward = cameraForward + new Vector3(inputHorizontal, 0, inputVertical);
//Quaternion targetRotation = Quaternion.LookRotation(moveForward - transform.position);
Quaternion targetRotation = Quaternion.LookRotation(moveForward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
rb.AddForce(new Vector3(inputHorizontal, 0, inputVertical) + moveSpeed * moveForward * 2, ForceMode.Impulse);
lastmov_f = transform.position;
}
カメラワークもだいぶマシになったが、加速度的に動きが早くなってしまうのでinputHorizontalなどの値が加算されているかと思ったが
左カーソルと下カーソルの動きがイマイチだったので、
Vector3 Dmoves = new Vector3(inputHorizontal, 0, inputVertical) + moveSpeed * moveForward * 2;
Debug.Log(Dmoves);
をスクリプトに追加してデバッグした値を確認した。
(3.0, -2.0, 0.0)
(0.3, 0.0, 0.0)
と出てきた。
YがマイナスなのはcameraForwardのy値のせいなのでここはcameraForward.y = 0;にする。
次に、
rb.AddForce(new Vector3(inputHorizontal, 0, inputVertical) + moveSpeed * moveForward * 2, ForceMode.Impulse);
を確認するとmoveForwardの値によっては、マイナス移動が相殺されることがわかる。Debug.Log("moveForward" + moveForward);を挿入。
きちんとmoveForward(-1.1, 0.0, 0.0)は向いている方向に対しての前方で、取得されていることがわかった。
であれば、むしろnew Vector3(inputHorizontal, 0, inputVertical) はいらないのではないかと考え一旦削除。
rb.AddForce(moveSpeed * moveForward * 3, ForceMode.Impulse);として観測していたら、諸々修正点をもつけたので一部コードを修正。
さらに、改造を加えたら、かなり別ものになってしまったが、下記で回転に対する正面をとって移動をさせ続けられるようになった。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Envplayermove3 : MonoBehaviour
{
Rigidbody rb;
float moveSpeed = 1f;
Vector3 lastmov_f;
void Start()
{
// Rigidbodyコンポーネントを取得する
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// Upキーで前に進む
if (Input.GetKey("up"))
{
Vector3 moveForward = transform.position + transform.forward * 100;
moveForward.y = 0;
Debug.Log("moveForward" + moveForward);
//Quaternion targetRotation = Quaternion.LookRotation(moveForward - transform.position);
Quaternion targetRotation = Quaternion.LookRotation(moveForward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
rb.position = transform.position + transform.forward * 10.0f * Time.deltaTime;
//MoveGo();
}
// Downキーで後ろに進む
if (Input.GetKey("down"))
{
Vector3 moveForward = transform.position - transform.forward * 100;
moveForward.y = 0;
Debug.Log("moveForward" + moveForward);
//Quaternion targetRotation = Quaternion.LookRotation(moveForward - transform.position);
Quaternion targetRotation = Quaternion.LookRotation(moveForward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
rb.position = transform.position - transform.forward * 10.0f * Time.deltaTime;
//MoveGo();
}
//right キーで右に進む
if (Input.GetKey("right"))
{
Vector3 moveForward = transform.position + transform.right * 100;
moveForward.y = 0;
Debug.Log("moveForward" + moveForward);
//Quaternion targetRotation = Quaternion.LookRotation(moveForward - transform.position);
Quaternion targetRotation = Quaternion.LookRotation(moveForward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
rb.position = transform.position + transform.right * 10.0f * Time.deltaTime;
Debug.Log(rb.position);
//MoveGo();
}
//left キーで左に進む
if (Input.GetKey("left"))
{
Vector3 moveForward = transform.position - transform.right * 100;
moveForward.y = 0;
Debug.Log("moveForward" + moveForward);
//Quaternion targetRotation = Quaternion.LookRotation(moveForward - transform.position);
Quaternion targetRotation = Quaternion.LookRotation(moveForward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
rb.position = transform.position - transform.right * 10.0f * Time.deltaTime;
//MoveGo();
}
}