简体   繁体   English

Unity C#CharacterController脚本

[英]Unity C# CharacterController Script

I have a simple script attached to a ball model in unity. 我有一个简单的脚本统一附加到球模型上。 In a effort to control the ball, I have attempted to mimic this example provided by the documentation. 在努力控制球,我试图模仿这种由文档提供的示例。 The issue I receive is my ball visually rotates half as fast as its physical rotation changes. 我收到的问题是,球的视觉旋转速度是其物理旋转速度的一半。

Ex: The ball will rotate visually 180 degrees when you make a 360 degree physics rotation. 例如:当您进行360度的物理旋转时,球将在视觉上旋转180度。

public class PlayerController : MonoBehaviour { 公共类PlayerController:MonoBehaviour {

public float MoveSpeed;
public float RotationSpeed;
CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal") * RotationSpeed, 0));
    Vector3 forward = Input.GetAxis("Vertical") * transform.TransformDirection(transform.forward) * MoveSpeed;

    controller.Move(forward);
}

What I would like to accomplish is for the ball to rotate in alignment with the rotation of its physics controls. 我要完成的是使球按照其物理控件的旋转方向旋转。

Just get rid of the transform.TransformDirection(transform.forward) , since you are trying to transform the local forward vector of the transform to the forward vector of the same transform. 只需摆脱transform.TransformDirection(transform.forward) ,因为您试图将转换的本地正向向量转换为同一转换的正向向量。

Just use a simple transform.forward instead. 只需使用简单的transform.forward

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM