Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将二维位置/旋转转换为三维_C#_Unity3d_Math - Fatal编程技术网

C# 将二维位置/旋转转换为三维

C# 将二维位置/旋转转换为三维,c#,unity3d,math,C#,Unity3d,Math,我有一个播放器对象,还有一个播放器和一个相机作为孩子连接在上面 我想围绕播放器旋转一个圆圈,使其始终面向播放器(以0,0,0为中心) 我有一个二维的方法,我需要转换为三维 这个脚本在3D中是什么样子的 多谢各位 using UnityEngine; using System.Collections; public class circularMotion : MonoBehaviour { public float RotateSpeed; public float Radius;

我有一个播放器对象,还有一个播放器和一个相机作为孩子连接在上面

我想围绕播放器旋转一个圆圈,使其始终面向播放器(以0,0,0为中心)

我有一个二维的方法,我需要转换为三维

这个脚本在3D中是什么样子的

多谢各位

 using UnityEngine;
 using System.Collections;

 public class circularMotion : MonoBehaviour {

 public float RotateSpeed;
 public float Radius;

 public Vector2 centre;
 public float angle;

 private void Start()
 {
     centre = transform.localPosition;
 }

 private void Update()
 {

     angle += RotateSpeed * Time.deltaTime;

     var offset = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * Radius;
     transform.localPosition = centre + offset;
 }
 }

一种方法是定义一个向上的向量,然后围绕相应的轴旋转

using UnityEngine;

public class circularMotion : MonoBehaviour
{
    public float RotateSpeed = 1;
    public float Radius = 1;

    public Vector3 centre;
    public float angle;

    public Vector3 upDirection = Vector3.up; // upwards direction of the axis to rotate around

    private void Start()
    {
        centre = transform.localPosition;
    }

    private void Update()
    {
        transform.up = Vector3.up;
        angle += RotateSpeed * Time.deltaTime;

        Quaternion axisRotation = Quaternion.FromToRotation(Vector3.up, upDirection);

        // position camera
        Vector3 offset = axisRotation * new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle)) * Radius;
        transform.localPosition = centre + offset;

        // look towards center
        transform.localRotation = axisRotation * Quaternion.Euler(0, 180 + angle * 180 / Mathf.PI, 0);
    }
}

一种方法是定义一个向上的向量,然后围绕相应的轴旋转

using UnityEngine;

public class circularMotion : MonoBehaviour
{
    public float RotateSpeed = 1;
    public float Radius = 1;

    public Vector3 centre;
    public float angle;

    public Vector3 upDirection = Vector3.up; // upwards direction of the axis to rotate around

    private void Start()
    {
        centre = transform.localPosition;
    }

    private void Update()
    {
        transform.up = Vector3.up;
        angle += RotateSpeed * Time.deltaTime;

        Quaternion axisRotation = Quaternion.FromToRotation(Vector3.up, upDirection);

        // position camera
        Vector3 offset = axisRotation * new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle)) * Radius;
        transform.localPosition = centre + offset;

        // look towards center
        transform.localRotation = axisRotation * Quaternion.Euler(0, 180 + angle * 180 / Mathf.PI, 0);
    }
}

您可能希望研究四元数-如果您不介意使用内置方法,您可以使用
Transform.LookAt
Transform.RotateAround
来创建所需的效果。@DogeAmazed我希望使用角度方法。您可能希望研究四元数-如果您不介意使用内置方法,您可以使用
Transform.LookAt
Transform.RotateAround
来创建所需的效果。@DogeAmazed我想使用角度方法。