zoukankan      html  css  js  c++  java
  • unity的一些算法(持续更新)

    跑酷篇

    获取两点之间的某点的位置

    startPoint为起始点

    endPoint为终点

    numZ是一个距离(例如:在这个路径上,startPoint.z+numZ 就是要获取的点的z)

    公式为:

      Vector3 getPoint= Vector3.Lerp(startPoint.position,endPoint.position,(numZ-startPoint.position.z)/(endPoint.position.z-startPoint.position.z));

      (注:ae/ab=cd/cb=ed/ac  三者的比例相同 , 其中 cd的值为numZ , cb的值为 endPoint.z-startPoint.z ,所以通过上列公式就可以得出指定路径中所需的指定numZ的那一个点 )

    两点之间的距离

    (A.position-B.position).sqrMagnitude或(A.position-B.position).magnitude 

    注:sqrMagnitude与magnitude 的区别,magnitude 直接得出距离值,sqrMagnitude得出的是距离的平方(?)不开根号,所以sqrMagnitude运算速度略快

    按照指定速度向目标点前进


    public float moveSpeed = 100f;//每秒的移动距离

    Vector3 targetPos ;//目标点
    Vector3 moveDir = targetPos - this.transform.position;//移动方向
    // moveDir.normalized * moveSpeed * Time.deltaTime 每帧的移动向量 【moveDir.normalized (将向量进行单位化,单位化就是把这个向量,方向不变,长度变为1)】
    transform.position += moveDir.normalized * moveSpeed * Time.deltaTime;

    摄像机跟随人物移动

       Transform player;
    
        Vector3 offset=Vector3.zero;//偏移量
    
        public float moveSpeed = 10f;//摄像机的移动速度
        void Awake()
        {
            player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    
            offset = transform.position - player.position ;
        }
    
        // Update is called once per frame
        void Update()
        {
            Vector3 targetPos = player.position + offset;
            transform.position= Vector3.Lerp(transform.position, targetPos, moveSpeed*Time.deltaTime);//参数顺序很重要
        }

    Animation.IsPlaying() 与 Animation.isPlaying 的区别 

    animation.IsPlaying() 与 animation.isPlaying 的区别,animation.IsPlaying()判断当前是否在播放某段动画,animation.isPlaying 判断当前是否正在播放动画(任何)

    详细来说就是,animation.IsPlaying("Idle_1") 判断当时是否正在播放 Idle_1 这段动画,animation.isPlaying判断当前Animation这个组件是否有在播放任意动画。

    Animation.Play()与Animation.PlayQueued()的区别

    animation.Play("Idle_1");是马上播放Idle_1
    animation.PlayQueued("Idle_2");在Idle_1播放完毕后,播放Idle_2

     获取鼠标点击位置

    Input.mousePosition

    跳跃(只控制上下,未设置前后方向的移动)

    在跳跃中,分成上升和下降两段过程

    首先需要设置几个变量值

        private Transform player;//要跳跃的物体
        public bool isJumping = false;//当前是否正处于跳跃状态
        private bool isUp=false;//当前是否正在上升(在跳跃状态中,上升时为true,下落时为false)
        public float jumpHeight = 20f;//最终要达到的跳跃高度
        public float jumpSpeed = 10f;//跳跃的升降速度
        float haveJumpHeight=0;//当前已经跳跃的高度

    设置完了以后要先获取到需要跳跃的物体,然后在触发跳跃时更改开关参数

        if (isJumping == false)//判断当前是否正处于跳跃状态
        {
              isJumping = true;
              isUp = true;
              haveJumpHeight = 0f;
        }

    根据开关参数来控制当前物体的跳跃

    void Update(){
         if (isJumping)
            {
                float yMove = jumpSpeed * Time.deltaTime;
                if (isUp)//上升过程
                {
                    player.position = new Vector3(player.position.x, player.position.y+yMove, player.position.z);
                    haveJumpHeight += yMove;
                    if(Mathf.Abs(jumpHeight - haveJumpHeight) <= 0.5f)
                    {
                        player.position = new Vector3(player.position.x, player.position.y + (jumpHeight - haveJumpHeight), player.position.z);
                        isUp = false;
                        haveJumpHeight = jumpHeight;
                    }
                }
                else//下降过程
                {
                    player.position = new Vector3(player.position.x, player.position.y - yMove, player.position.z);
                    haveJumpHeight -= yMove;
                    if (Mathf.Abs(haveJumpHeight) <= 0.5f)
                    {
                        player.position = new Vector3(player.position.x, player.position.y -  haveJumpHeight, player.position.z);
                        isJumping = false;
                        haveJumpHeight = 0f;
                    }
                }
            }
    }
  • 相关阅读:
    证书介绍
    Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
    Hive安装与配置详解
    技术学习内容
    死锁,更新锁,共享锁,排它锁,意向锁,乐观锁,悲观锁等名词解释及案例详解
    死锁语句
    SQL Server 锁表、查询被锁表、解锁相关语句
    Psi Probe 安装及使用说明
    PowerDesigner使用教程
    Python -面向对象(一 基本概念)
  • 原文地址:https://www.cnblogs.com/lingLuoChengMi/p/11983021.html
Copyright © 2011-2022 走看看