C# 如何在unity c中围绕播放器旋转摄影机#
我需要在按住鼠标左键的同时围绕我的玩家游戏对象旋转相机。我将如何处理这个问题 另外,我也读了一些关于向量3的内容,但我对它没有完全的理解。任何能解释的人都将不胜感激 我看过youtube视频,这正是我想要的概念。我只是在把它应用到我的代码时遇到了麻烦 我的时间有点紧,考试快到了,我的老师没有解释视频中解释的大部分内容 //这是我在摄像机内的代码,它跟踪球/球员C# 如何在unity c中围绕播放器旋转摄影机#,c#,unity3d,C#,Unity3d,我需要在按住鼠标左键的同时围绕我的玩家游戏对象旋转相机。我将如何处理这个问题 另外,我也读了一些关于向量3的内容,但我对它没有完全的理解。任何能解释的人都将不胜感激 我看过youtube视频,这正是我想要的概念。我只是在把它应用到我的代码时遇到了麻烦 我的时间有点紧,考试快到了,我的老师没有解释视频中解释的大部分内容 //这是我在摄像机内的代码,它跟踪球/球员 using System.Collections; using System.Collections.Generic; using Un
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);
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类脚本球:单一行为
{
公众浮标速度;
私人刚体;
void Start()
{
rb=GetComponent();
}
void FixedUpdate()
{
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
Vector3移动=新Vector3(水平移动,0.0f,垂直移动);
rb.AddForce(移动*速度);
}
//结束代码
我期待的结果正好显示在1:22英寸
试试这个。脚本可以放在你的相机上 基本上,该脚本首先获取鼠标移动的方向。在本例中,X轴
鼠标X
(左/右方向)。然后我们采用旋转速度旋转速度
,并使用旋转速度围绕播放器旋转一定程度的度数。最后,我们通过使用变换确保相机始终注视着播放器。LookAt
using UnityEngine;
using System.Collections;
public class OrbitPlayer : MonoBehaviour {
public float turnSpeed = 5.0f;
public GameObject player;
private Transform playerTransform;
private Vector3 offset;
private float yOffset = 10.0f;
private float zOffset = 10.0f;
void Start () {
playerTransform = player.transform;
offset = new Vector3(playerTransform.position.x, playerTransform.position.y + yOffset, playerTransform.position.z + zOffset);
}
void FixedUpdate()
{
offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
transform.position = playerTransform.position + offset;
transform.LookAt(playerTransform.position);
}
}
这里有很多关于这个主题的信息:
如果你看1:22前几秒钟的最后一段视频,它显示了一些关于如何解决问题的代码,这是四元数的内容,即“不完全理解[Vector3]”。我假设你指的是一般的向量。花点时间学习向量运算是非常值得的。它们非常有用(我可能有偏见——我学的是机械工程)。我在我的项目中使用
Transform.RotateAround
环绕一个点。它为你处理所有的变换。