示例#1
0
    void DoRaycasting()
    {
        // Cast a ray straight down.
        hit = Physics2D.Raycast(transform.position, -spriteObj.transform.up, distance, hitLayers);



        // If it hits something...
        if (hit.collider != null)
        {
            Debug.DrawRay(hit.point, hit.normal, Color.red);
            //Debug.DrawRay(transform.position, -transform.up * distance, Color.blue);
            //Debug.DrawLine(hit.point, hit.normal, Color.red);
            Debug.DrawLine(transform.position, hit.point, Color.blue);



            if (!isGrounded)
            {
                isGrounded = true;
                gameManagerScript.CallMyCanControl(true);
            }

            colPos = hit.point + hit.normal * -distance;

            vectorToTarget = colPos - transform.position;
            angle          = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
            q = Quaternion.AngleAxis(angle, transform.forward);
            q = Quaternion.Euler(0, 0, 90) * q;
            spriteObj.transform.rotation = Quaternion.Slerp(spriteObj.transform.rotation, q, Time.deltaTime * 10f);
            //transform.rotation = Quaternion.Slerp(spriteObj.transform.rotation, q, Time.deltaTime * 10f);

            float distanceGround = Vector3.Distance(transform.position, hit.point);

            //myBody.AddForce(hit.normal * (-40f * (distance - distanceGround)), ForceMode2D.Force);
            myBody.AddForce(hit.normal * (-10f * myBody.velocity.magnitude), ForceMode2D.Force);
        }
        else
        {
            if (isGrounded)
            {
                isGrounded = false;
                gameManagerScript.CallMyCanControl(false);
            }
            colPos         = transform.position + Vector3.forward * -distance;
            vectorToTarget = colPos - transform.position;
            angle          = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
            q = Quaternion.AngleAxis(angle, transform.forward);
            q = Quaternion.Euler(0, 0, 0) * q;
            spriteObj.transform.rotation = Quaternion.Slerp(spriteObj.transform.rotation, q, Time.deltaTime * 10f);
        }
    }