【问题标题】:How to rotate camera around player in unity c#如何在统一C#中围绕播放器旋转相机
【发布时间】: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

https://www.youtube.com/watch?v=xcn7hz7J7sI

【问题讨论】:

  • 如果您在 1:22 前几秒钟观看最后一个视频,它显示了一些有关如何解决您的问题的代码,那就是四元数
  • “不完全了解 [Vector3]”我假设您的意思是一般的向量。学习一点向量运算是非常值得的。它们非常有用(我可能有偏见——我学的是机械工程)。我在我的项目中使用Transform.RotateAround 围绕一个点进行轨道运行。它为您处理所有转换。

标签: c# unity3d


【解决方案1】:

试试这个。脚本在你的相机上。

基本上,此脚本通过首先获取鼠标移动的方向来工作。在这种情况下,X 轴Mouse X(左/右方向)。然后我们使用我们的旋转速度turnSpeed,并使用它使用Quaternion.AngleAxis 围绕玩家旋转该度数。最后,我们使用transform.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);
     }  
}

这里有很多关于这个主题的信息: https://answers.unity.com/questions/600577/camera-rotation-around-player-while-following.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 2017-06-06
    • 1970-01-01
    • 2019-01-26
    • 2010-09-22
    • 2014-11-01
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多