如何解决如何始终将方向更改为旋转角度?
我在下面有这个脚本,我希望我的播放器总是朝着旋转角度移动,这不会发生。它只会在我点击时改变方向。
我的目的是让玩家始终朝着应该由鼠标位置/鼠标 x 轴控制的旋转方向移动(有点像自动运行,但始终根据鼠标更改旋转,而不仅仅是向右或左)。
我已经尝试了大约 10 种不同的方法,到目前为止没有任何效果......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehavIoUr
{
public float speed = 4;
public float rot = 0f;
public float rotSpeed = 80;
public float gravity = 8;
private Camera cam;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController> ();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float horizontalSpeed = 8.0f;
//Get the mouse delta. This is not in the range -1...1
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float z = horizontalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(0,h,0);
//Move Input
if(controller.isGrounded){
if(Input.GetMouseButtonUp(1))
{
anim.SetInteger ("condition",1);
moveDir = new Vector3 (0,1) * speed;
// moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
if(Input.GetMouseButtonDown(1))
{
anim.SetInteger("condition",0);
moveDir = Vector3.zero;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
解决方法
Transform.LookAt 只需获取光标位置即可
,Vector3 direction = target.position - player.transform.position;
Quaternion finalPlayerRotation = Quaternion.LookRotation(direction);
player.transform.rotation = finalPlayerRotation;
这在某些情况下也有效:
Vector3 direction = target.position - player.transform.position;
player.transform.right /*You may need to change the Right to upper,-upper,-Right depend on the player rotation and the target position*/ = direction;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。