【发布时间】:2020-10-29 11:23:11
【问题描述】:
正如您在此视频中看到的那样,对象向任何方向移动,但其模型不会沿移动方向旋转。怎么解决??
视频链接
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour
{
private CharacterController controller = null;
private Animator animator = null;
private float speed = 5f;
void Start()
{
controller = gameObject.GetComponent<CharacterController>();
animator = gameObject.GetComponent<Animator>();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = (transform.right * x) + (transform.forward * z);
controller.Move(move * speed * Time.deltaTime);
var angle = Mathf.Atan2(move.z, move.x) * Mathf.Rad2Deg;
if (x != 0 || z != 0) animator.SetTrigger("run");
if (x == 0 && z == 0) animator.SetTrigger("idle");
}
}
【问题讨论】:
-
没有代码,不可能知道你做了什么让它转动或没有做什么
-
试试这个:
Vector3 move = new Vector3(x, 0f, z);transform.forward = move;controller.Move(move * speed * Time.deltaTime); -
一切都很好。你可以创建一个答案,我会接受它
标签: c# unity3d game-engine