Unity3d 角色跳跃

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class SpaceJump : MonoBehaviour
{

    public float distance = 5f;
    public float hight = 3f;
    public float useTime = 1.5f;

    public float rigidbodyForce = 7f;

   

    // Use this for initialization
    void Jump()
    {

        Vector3 p2;//角色落点
        Vector3 p1;//最高点

        p1 = transform.position + transform.forward * (distance / 2) + transform.up * hight;//第二个点:起始位置前方几米处上方几米的坐标.

        p2 = transform.position + Vector3.forward * distance;
        //找到碰撞器 落脚点
        RaycastHit raycastHit; //碰撞体信息
        if (Physics.Raycast(p2, Vector3.down, out raycastHit, 81f))
        {
            //raycastHit.point 碰撞到的点
            //碰撞到的物体  raycastHit.transform.gameObject
            p2 = raycastHit.point;
        }


        Vector3[] paths = new Vector3[3];
        paths[0] = transform.position;
        paths[1] = p1;
        paths[2] = p2;
        iTween.MoveTo(gameObject, iTween.Hash("path", paths, "movetopath", true, "time", useTime));//movetopath=false按点移动,=true按照点构成的曲线路径移动
    }

    void RigidbodyJump()
    {
        Rigidbody rigidbody = GetComponent<Rigidbody>();

        //打开刚体移动
        rigidbody.constraints = RigidbodyConstraints.None;
        rigidbody.constraints = RigidbodyConstraints.FreezeRotation;

        //计算角色跳向鼠标位置的方向
        Vector3 dir = Vector3.forward;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray, out hit))
        {
            Vector3 hitPos = hit.point;
            Vector3 playerPos = transform.position;
            //向量减法 和y无关,所以同步一下高度
            playerPos.y = hitPos.y;
            //向量减法,得到一个向量,包含方向和距离
            dir = (hitPos - playerPos);
            //归一化 去除距离 ,只要方向,如果不去除距离 ,那么角色闪到鼠标点击的位置
            dir = dir.normalized;
        }

        //角色当前刚体速度
        float xSpeed =0;
        float zSpeed = 0;
        if (GetComponent<NavMeshAgent>()==null)
        {
             xSpeed = rigidbody.velocity.x;
             zSpeed = rigidbody.velocity.z;

        }
        else
        {
            xSpeed = GetComponent<NavMeshAgent>().velocity.x;
            zSpeed = GetComponent<NavMeshAgent>().velocity.z;
        }


        if(xSpeed<=0 && zSpeed<=0)
        {//向上眺
            rigidbody.velocity +=  transform.up * hight;
        }
        else 
        {//角色身上有速度的话,向鼠标方向上跳
            rigidbody.velocity += dir * distance + transform.up * hight;
        }

        rigidbody.AddForce(Vector3.up * rigidbodyForce);
    }
    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if(GetComponent<Rigidbody>()==null)
            {
                Jump();
            }
            else
            {
                RigidbodyJump();
            }
            
        }

    }
}

猜你喜欢

转载自blog.csdn.net/u013628121/article/details/81252350