Exemplo n.º 1
0
    void Update()
    {
        if (!playerMovement.isGrounded)
        {
            rigbod.AddForce(gravity.GetForce());
        }

        playerForward = masterForwardObject.transform.position - playerBodyShell.transform.position;
        playerForward = playerForward.normalized;
        playerRight   = masterRightObject.transform.position - playerBodyShell.transform.position;
        playerRight   = playerRight.normalized;
        playerUp      = masterUpObject.transform.position - playerBodyShell.transform.position;
        playerUp      = playerUp.normalized;


        float dot = Vector3.Dot(-gravity.GravityDirection, playerMovement.playerForward) / -gravity.GravityDirection.magnitude;

        Vector3 newPos = playerMovement.playerForwardObject.transform.position + (-gravity.GravityDirection * dot);

        Vector3 newForward = newPos - playerBodyShell.transform.position;

        newForward = newForward.normalized;


        if (debugStuff.debugPlayerGravity)
        {
            Debug.DrawRay(playerBodyShell.transform.position, newForward * 11000, Color.blue);
            Debug.DrawRay(playerBodyShell.transform.position, -gravity.GravityDirection * 11000, Color.green);
            Debug.DrawRay(playerBodyShell.transform.position, playerMovement.playerRight * 11000, Color.red);
            Debug.DrawRay(playerBodyShell.transform.position, playerMovement.playerForward * 11000, Color.cyan);
            Debug.DrawRay(playerMovement.playerForwardObject.transform.position, -gravity.GravityDirection * dot, Color.yellow);
        }

        if (rigbod.velocity.magnitude > 0.1 && !playerMovement.isGrounded)
        {
            Quaternion dirQ = Quaternion.LookRotation(newForward, -gravity.GravityDirection);                                                                  // what direction i want

            Quaternion slerp = Quaternion.Slerp(playerBodyShell.transform.rotation, dirQ, rigbod.velocity.magnitude / gravity.RotationSpeed * Time.deltaTime); // rotates to it over time

            if (debugStuff.debugPlayerGravity)
            {
                Debug.Log("Forward: " + newForward + " / Up: " + -gravity.GravityDirection);
                Debug.Log("dirQ: " + dirQ.eulerAngles);
                Debug.Log("PlayerBody Rotation: " + playerBodyShell.transform.rotation.eulerAngles + " / slerp: " + slerp.eulerAngles);
            }

            if (isRotating)
            {
                playerBodyShell.transform.rotation = slerp;
            }
        }
    }
Exemplo n.º 2
0
    // Update is called once per frame
    void LateUpdate()
    {
        var pos = new Vector2(transform.position.x, transform.position.z);

        if (SnapPosition)
        {
            transform.position = new Vector3(pos.x, Gravity.GetHeight(pos) + Offset, pos.y);
        }
        if (SnapRotation)
        {
            var normal  = Gravity.GetNormal(pos, GravityGradientStep, NormalMultiplier);
            var forward = Vector3.Cross(transform.right, normal);
            transform.rotation = Quaternion.LookRotation(forward, normal);
        }

        if (ApplyForces)
        {
            //var currentVelocity = _body.velocity;
            var force = Gravity.GetForce(pos) * ForceScale;
            _body.AddForce(force.x, 0, force.y, ForceMode.Acceleration);
        }
    }