【发布时间】:2019-06-01 03:50:55
【问题描述】:
我需要在按住鼠标左键的同时围绕我的玩家游戏对象旋转相机。我该如何处理?
另外,我读过一些关于 Vector 3 的内容,但我对它没有完全理解。任何能解释它的人将不胜感激。
我看过 youtube 视频,this one 正是我正在寻找的概念。我只是无法将它应用到我的代码中。
我的时间有点紧,考试快到了,我的老师还没有解释视频中解释的大部分内容。
// 这是我在相机中的代码,它跟随球/球员
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptBallCam : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
//相机内代码结束
//球员/球内的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptBall : MonoBehaviour
{
public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
//结束代码
我期待的结果准确地显示在 1:22
【问题讨论】:
-
如果您在 1:22 前几秒钟观看最后一个视频,它显示了一些有关如何解决您的问题的代码,那就是四元数
-
“不完全了解 [Vector3]”我假设您的意思是一般的向量。学习一点向量运算是非常值得的。它们非常有用(我可能有偏见——我学的是机械工程)。我在我的项目中使用
Transform.RotateAround围绕一个点进行轨道运行。它为您处理所有转换。