当球员向南移动时,球员开始在拥有自身引力的行星上跳跃

时间:2017-09-27 22:55:04

标签: c# unity3d

所以我有一个拥有自身重力的行星,我可以在顶部移动很好但是当我向南走时,玩家因某种原因开始上下跳跃,我不知道为什么关于如何解决它的想法? Video of problem

行星引力

void OnTriggerStay(Collider other)
{
    if (other.gameObject.GetComponent<Rigidbody>())
    {
        Vector3 difference = this.gameObject.transform.position - other.gameObject.transform.position;

        float dist = difference.magnitude;
        Vector3 gravityDirection = difference.normalized;
        Vector3 gravityVector = (gravityDirection * gravity) / (dist * dist);
        other.gameObject.GetComponent<Rigidbody>().AddForce(gravityVector, ForceMode.Acceleration);

    }
}

运动

    void Update ()
{
    ////Defaults to the left Stick
   float hAxis = Input.GetAxis("Horizontal"); 
   float vAxis = Input.GetAxis("Vertical");


    Vector3 NextDir = new Vector3(hAxis, 0, vAxis);

    float deadzone = 0.25f;
    //Vector2 stickInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

    if (NextDir.magnitude < deadzone)
    {
        NextDir = Vector3.zero;


    }
    else
    {
        NextDir = NextDir.normalized * ((NextDir.magnitude - deadzone) / (1 - deadzone));
        transform.rotation = Quaternion.LookRotation(NextDir);
        transform.Translate(NextDir.x * Time.deltaTime * 5, NextDir.y * Time.deltaTime * 5, NextDir.z * Time.deltaTime * 5, Space.Self);


    }
 }
}

玩家定位脚本

    void LateUpdate()
{

    Vector3 dir = planet.transform.position - transform.position;
    dir.Normalize();
    RaycastHit hit;
    if (Physics.Raycast(transform.position, dir * 100, out hit))
    {

        transform.localRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;

    }

}

0 个答案:

没有答案