广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    Unity 物体运动 之 物体按规定路线运动的优化

    来源:网络收集  点击:  时间:2024-02-21
    【导读】:
    Unity 物体运动 之 物体按规定路线运动的优化。本节针对之前已有的《Unity 物体运动 之 物体按规定路线运动的使用》(链接如下)的不足,物体运动朝向不对的问题,进行优化的简单案例,具体如下工具/原料moreUnity一、知识要点1/2分步阅读

    Vector3.MoveTowards:

    1)功能简述

    public staticVector3MoveTowards(Vector3current,Vector3target, floatmaxDistanceDelta);

    Moves a pointcurrentin a straight line towards atargetpoint.

    The value returned by this function is a pointmaxDistanceDeltaunits closer to atarget/point along a line betweencurrentandtarget. If the target is closer thanmaxDistanceDelta/then the returned value will be equal to target (ie, the movement will not overshoot the target). Negative values ofmaxDistanceDeltacan be used to push the point away from the target.

    2)使用举例

    using UnityEngine;

    using System.Collections;public class ExampleClass : MonoBehaviour {

    public Transform target;

    public float speed;

    void Update() {

    float step = speed * Time.deltaTime;

    transform.position = Vector3.MoveTowards(transform.position, target.position, step);

    }

    }

    2/2

    Transform.LookAt:

    1)功能简述

    public voidLookAt(Transformtarget,Vector3worldUp= Vector3.up);

    Parameters

    target:Object to point towards.

    worldUp:Vector specifying the upward direction.

    Rotates the transform so the forward vector points at /target/s current position.

    Then it rotates the transform to point its up direction vector in the direction hinted at by theworldUpvector. If you leave out theworldUpparameter, the function will use the world y axis.worldUpis only a hint vector. The up vector of the rotation will only match theworldUpvector if the forward direction is perpendicular toworldUp.

    2)使用举例

    // This complete script can be attached to a camera to make it

    // continuously point at another object.// The target variable shows up as a property in the inspector.

    // Drag another object onto it to make the camera look at it.

    using UnityEngine;

    using System.Collections;public class ExampleClass : MonoBehaviour{

    public Transform target; void Update() {

    // Rotate the camera every frame so it keeps looking at the target

    transform.LookAt(target);

    }

    }

    二、物体运动 之 物体按规定路线运动的优化1/10

    在Unity场景中新建一个“Plane”和“Cube”是低昂调整大小与布局,“Maain Camera”的视角调整好,具体如下图

    2/10

    在场景中添加路线的关键点,演示需要大致随意摆放几个点,具体如下图

    3/10

    在工程中,仙剑两个脚本“Move”和“WayPoints”,代开脚本进行编辑,具体如下图

    4/10

    在“WayPoints”脚本上编辑代码,具体代码和代码说明如下图

    5/10

    “WayPoints”脚本具体内容如下:

    using UnityEngine;

    public class WayPoints : MonoBehaviour

    {

    public static Transform wayPoints;

    void Awake()

    {

    int count = transform.childCount;

    wayPoints = new Transform;

    for (int i = 0; i count; i++)

    {

    wayPoints = transform.GetChild(i);

    }

    }

    }

    6/10

    在“Move”脚本上编辑代码,本脚本关键使用LookAt朝向目标,然后MoveTowards在移动到目标点,具体代码和代码说明如下图

    7/10

    “Move”脚本具体内容如下:

    using UnityEngine;

    public class Move : MonoBehaviour

    {

    public float speed = 5;

    private Transform ways;

    private int index;

    // Use this for initialization

    void Start(){

    ways = WayPoints.wayPoints;

    index = 0;

    }

    // Update is called once per frame

    void Update(){

    MoveTo();

    }

    void MoveTo(){

    if (index ways.Length - 1)

    return;

    //保证正面朝向物体运动

    transform.LookAt(ways.position);

    transform.position = Vector3.MoveTowards(transform.position,

    ways.position, Time.deltaTime * speed);

    if (Vector3.Distance(ways.position, transform.position) 0.2f){

    index++;

    if (index == ways.Length){

    transform.position = ways.position;

    }

    }

    }

    }

    8/10

    脚本编译正确,回到Unity界面,“WayPoints”脚本赋给场景中的“WayPoints”,“Move”脚本赋给“Cube”

    9/10

    运行场景,可以看到,物体首先朝向目标点,然后在向目标点运动,弥补朝向不变的不足,具体如下图

    10/10

    到此,《Unity 物体运动 之 物体按规定路线运动的优化》讲解结束,谢谢

    注意事项

    您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

    UNITY物体路线运动面朝路线点运动
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_91575.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-06 20:37:38  耗时:0.031
    0.0311s