zoukankan      html  css  js  c++  java
  • 物体运动学习笔记(一)

    跟着CatLike大神的教程敲了一遍,记录下学习心得。地址:https://catlikecoding.com/unity/tutorials/ 

    通过Transform的position移动。

    一、通过加速度限制运动物体的速度改变量可以使物体运动更平滑

         float maxSpeedChange = maxAcceleration * Time.deltaTime;
            velocity.x = Mathf.MoveTowards(velocity.x,desiredVelocity.x,maxSpeedChange);
            velocity.z = Mathf.MoveTowards(velocity.z,desiredVelocity.z,maxSpeedChange);

    二、运动物体撞到障碍物反弹速度=-物体速度*障碍物的反弹系数

            if (newPos.x < allowedArea.xMin)
            {
                newPos.x = allowedArea.xMin;
                velocity.x = -velocity.x * bounciness;
            }
            else if (newPos.x > allowedArea.xMax)
            {
                newPos.x = allowedArea.xMax;
                velocity.x = -velocity.x * bounciness;
            }
            if (newPos.z < allowedArea.yMin)
            {
                newPos.z = allowedArea.yMin;
                velocity.z = -velocity.z * bounciness;
            }
            else if (newPos.z > allowedArea.yMax)
            {
                newPos.z = allowedArea.yMax;
                velocity.z = -velocity.z * bounciness;
            }

    完整代码:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MovingSphere : MonoBehaviour
    {
        [SerializeField,Range(0,10)]
        float maxSpeed = 10;
        [SerializeField, Range(0, 10)]
        float maxAcceleration = 10;
        Vector3 velocity;
        [SerializeField]  //移动范围
        Rect allowedArea = new Rect(-5f,-5f,10f,10f);
        [SerializeField, Range(0f, 1f)]  //反弹系数
        float bounciness = 0.5f;
        private void Update()
        {
            Vector2 playerInput;
            playerInput.x = Input.GetAxis("Horizontal");
            playerInput.y = Input.GetAxis("Vertical");
            playerInput = Vector2.ClampMagnitude(playerInput,1f);
            Vector3 desiredVelocity = new Vector3(playerInput.x,0,playerInput.y)*maxSpeed;
            float maxSpeedChange = maxAcceleration * Time.deltaTime;
            velocity.x = Mathf.MoveTowards(velocity.x,desiredVelocity.x,maxSpeedChange);
            velocity.z = Mathf.MoveTowards(velocity.z,desiredVelocity.z,maxSpeedChange);
            Vector3 displacement = velocity * Time.deltaTime;
            Vector3 newPos = transform.localPosition + displacement;
            if (newPos.x < allowedArea.xMin)
            {
                newPos.x = allowedArea.xMin;
                velocity.x = -velocity.x * bounciness;
            }
            else if (newPos.x > allowedArea.xMax)
            {
                newPos.x = allowedArea.xMax;
                velocity.x = -velocity.x * bounciness;
            }
            if (newPos.z < allowedArea.yMin)
            {
                newPos.z = allowedArea.yMin;
                velocity.z = -velocity.z * bounciness;
            }
            else if (newPos.z > allowedArea.yMax)
            {
                newPos.z = allowedArea.yMax;
                velocity.z = -velocity.z * bounciness;
            }
            transform.localPosition = newPos;
        }
    }
    

      

  • 相关阅读:
    LINUX VNC配置[转]
    win7下,两台笔记本内置的无线局域网卡共享上网
    修改Linux和aix系统为北京时区
    ubuntu 9.10 下安装ORACLE 出错SP20750: You may need to set ORACLE_HOME to your Oracle software directory
    安装vim7.1
    select 语句的处理顺序
    批量从数据库是提取数据,并显示出来。
    输入四个字符串然后按大到后输出。
    shell 批量修改指定的文件。
    unix自动登录Telnet,实现查看多台服务器硬盘及数据表空间使用情况
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14322258.html
Copyright © 2011-2022 走看看