旋转刚体控制器

时间:2017-12-17 14:25:14

标签: c# unity3d

有我的脚本Rigidbody控制器 -

public float Speed = 5f;
public float JumpHeight = 2f;
public float GroundDistance = 0.2f;
public float DashDistance = 5f;
public LayerMask Ground;

private Rigidbody _body;
private Vector3 _inputs = Vector3.zero;
private bool _isGrounded = true;
private Transform _groundChecker;


void Start()
{
    _body = GetComponent<Rigidbody>();
    _groundChecker = transform.GetChild(0);
}

void Update()
{
    _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);


    _inputs = Vector3.zero;
    _inputs.x = Input.GetAxis("Horizontal");
    _inputs.z = Input.GetAxis("Vertical");
    if (_inputs != Vector3.zero)
        transform.forward = _inputs;

    if (Input.GetButtonDown("Jump") && _isGrounded)
    {
        _body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
    }
    if (Input.GetButtonDown("Sprint"))
    {
        Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new Vector3((Mathf.Log(1f / (Time.deltaTime * _body.drag + 1)) / -Time.deltaTime), 0, (Mathf.Log(1f / (Time.deltaTime * _body.drag + 1)) / -Time.deltaTime)));
        _body.AddForce(dashVelocity, ForceMode.VelocityChange);
    }  
}


void FixedUpdate()
{
    _body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime);
}

在相机方向上转动y的最佳方法是什么?也就是说,玩家转向鼠标转动的一侧?它是在fixedUpdate还是更新?

这是相机脚本:

public float Smoothness = 0.3F;
public Vector2 Sensitivity = new Vector2(4, 4);
public Vector2 LimitX = new Vector2(-70, 80);

private Vector2 NewCoord;
public Vector2 CurrentCoord;
private Vector2 vel;
public GameManager GameMangerS;

public Transform Target;
public float TransformSpeed;

public Animator CameraAnimator;

void Update()
{
    NewCoord.x = Mathf.Clamp(NewCoord.x, LimitX.x, LimitX.y);
    NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x;
    NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y;
    CurrentCoord.x = Mathf.SmoothDamp(CurrentCoord.x, NewCoord.x, ref vel.x, Smoothness / 2);
    CurrentCoord.y = Mathf.SmoothDamp(CurrentCoord.y, NewCoord.y, ref vel.y, Smoothness / 2);
    transform.rotation = Quaternion.Euler(CurrentCoord.x, CurrentCoord.y, 0);
}

并将此行添加到控制器脚本 -

void FixedUpdate()
{
    _body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime);
    transform.rotation = Quaternion.Euler(0, MainCamera.CurrentCoord.y, 0);
}

当我站立时,播放器正常旋转,但当我开始移动时,所有旋转都会重置并且播放器不会移动。

1 个答案:

答案 0 :(得分:0)

<强>更新

使用transform.Rotate()。

可以实现简单旋转

示例:

this.transform.Rotate(Vector3.up, 30);

此示例将围绕向上指向的Vector旋转变换30°。

<强> LookAtMouse:

要使对象转向鼠标,需要使用相机的ScreenToWorldSpace()方法。为了将ScreenCoordinates转换为您的WorldCoordinates。

示例:

请注意:

  
      
  1. 您必须设置相机实例!如果你不添加它,你将得到一个NullReferenceException。
  2.   
  3. 此片段只显示实现您希望的行为所需的步骤。
  4.   
  5. 您必须自己了解如何将代码行集成到代码中以使其正常工作。考虑一下程序员在整合评论时在评论中告诉你的内容。
  6.   
Vector3 mousePosition = Input.mousePosition; //get the screenSpaceMousePosition
Vector3 worldPosition = this.camera.ScreenToWorldPoint(mousePosition); //convert it into worldSpacePosition
Vector3 calculatedDirection = worldPosition - transform.position; //calucate the looking direction
calculatedDirection.y = 0; 
Quaternion rotation = Quaternion.LookRotation(calculatedDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);