Unity3D C#。使用 transform.RotateAround() 函数同时保持两个对象之间的距离不变
Unity3D C#. Using the transform.RotateAround() function while keep the distance between two objects constant
我正在尝试练习在 Unity 3D 中制作第三人称控制器。我是初学者,对如何使我的控制器发挥作用完全感到困惑。我一直在做数小时的研究,但我找不到任何线索似乎可以回答我的问题。我有两个脚本,一个 CameraController 和一个 CharacterController。我的代码如下。
相机控制器:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject target;
public float rotationSpeed;
Vector3 offset;
Vector3 CameraDestination;
// Use this for initialization
void Start () {
offset = transform.position - target.transform.position;
CameraDestination = offset + transform.position;
rotationSpeed = 50f;
transform.position = CameraDestination;
}
// Update is called once per frame
void Update () {
transform.LookAt (target.transform.position);
float h = Input.GetAxisRaw ("Horizontal");
transform.RotateAround (target.transform.position, Vector3.up, Time.deltaTime * h * rotationSpeed);
target.transform.Rotate (0f, Time.deltaTime * h * rotationSpeed, 0f);
}
}
角色控制器:
using UnityEngine;
using System.Collections;
public class CharController : MonoBehaviour {
public float playerSpeed = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float Vertical = Input.GetAxis("Vertical");
transform.position += transform.forward * Time.deltaTime * playerSpeed * Vertical;
}
}
当按下左箭头键或右箭头键时,播放器和相机都会旋转。如果我小时候尝试将相机连接到播放器,相机的旋转就会变得混乱,但如果我不将相机连接到播放器,相机就会停止跟随播放器。如果我尝试将相机设置到相对于玩家的特定位置,它会停止像预期的那样围绕玩家旋转。我实在想不出一个方法works.Thank你提前回答我的问题!
当我这样做时,我喜欢有一个空的游戏对象,它有 2 个 children,相机,然后是角色的网格。
> CharacterController
> Camera
> CharacterRig
当你想旋转角色时,旋转 CharacterController,然后当你想围绕角色旋转 Camera 时,将代码更改为:
transform.RotateAround (CharacterRig.transform.position, Vector3.up, Time.deltaTime * h * rotationSpeed);
这将允许您的相机旋转而不管任何角色动画,应该可以解决您的问题。如果您想稍后实现动画,这一点非常重要,因为您不想 parent 将相机对准正在动画的物体,因为它会随着动画移动。
P.s。您的代码看起来很好。当然,这纯粹是您设置游戏的方式 objects。
我正在尝试练习在 Unity 3D 中制作第三人称控制器。我是初学者,对如何使我的控制器发挥作用完全感到困惑。我一直在做数小时的研究,但我找不到任何线索似乎可以回答我的问题。我有两个脚本,一个 CameraController 和一个 CharacterController。我的代码如下。
相机控制器:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject target;
public float rotationSpeed;
Vector3 offset;
Vector3 CameraDestination;
// Use this for initialization
void Start () {
offset = transform.position - target.transform.position;
CameraDestination = offset + transform.position;
rotationSpeed = 50f;
transform.position = CameraDestination;
}
// Update is called once per frame
void Update () {
transform.LookAt (target.transform.position);
float h = Input.GetAxisRaw ("Horizontal");
transform.RotateAround (target.transform.position, Vector3.up, Time.deltaTime * h * rotationSpeed);
target.transform.Rotate (0f, Time.deltaTime * h * rotationSpeed, 0f);
}
}
角色控制器:
using UnityEngine;
using System.Collections;
public class CharController : MonoBehaviour {
public float playerSpeed = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float Vertical = Input.GetAxis("Vertical");
transform.position += transform.forward * Time.deltaTime * playerSpeed * Vertical;
}
}
当按下左箭头键或右箭头键时,播放器和相机都会旋转。如果我小时候尝试将相机连接到播放器,相机的旋转就会变得混乱,但如果我不将相机连接到播放器,相机就会停止跟随播放器。如果我尝试将相机设置到相对于玩家的特定位置,它会停止像预期的那样围绕玩家旋转。我实在想不出一个方法works.Thank你提前回答我的问题!
当我这样做时,我喜欢有一个空的游戏对象,它有 2 个 children,相机,然后是角色的网格。
> CharacterController
> Camera
> CharacterRig
当你想旋转角色时,旋转 CharacterController,然后当你想围绕角色旋转 Camera 时,将代码更改为:
transform.RotateAround (CharacterRig.transform.position, Vector3.up, Time.deltaTime * h * rotationSpeed);
这将允许您的相机旋转而不管任何角色动画,应该可以解决您的问题。如果您想稍后实现动画,这一点非常重要,因为您不想 parent 将相机对准正在动画的物体,因为它会随着动画移动。
P.s。您的代码看起来很好。当然,这纯粹是您设置游戏的方式 objects。