UNITY 移动到指定位置的写法

简介: //move towards a target at a set speed. private void MoveTowardsTarget() { //the speed, in units per second, we want to move towards the targe...
//move towards a target at a set speed.
private void MoveTowardsTarget() {
    //the speed, in units per second, we want to move towards the target
    float speed = 1;
    //move towards the center of the world (or where ever you like)
    Vector3 targetPosition = new Vector3(0,0,0);

    Vector3 currentPosition = this.transform.position;
    //first, check to see if we're close enough to the target
    if(Vector3.Distance(currentPosition, targetPosition) > .1f) { 
        Vector3 directionOfTravel = targetPosition - currentPosition;
        //now normalize the direction, since we only want the direction information
        directionOfTravel.Normalize();
        //scale the movement on each axis by the directionOfTravel vector components

        this.transform.Translate(
            (directionOfTravel.x * speed * Time.deltaTime),
            (directionOfTravel.y * speed * Time.deltaTime),
            (directionOfTravel.z * speed * Time.deltaTime),
            Space.World);
    }
}

 

目录
打赏
0
0
0
0
13
分享
相关文章
JS中的数组有哪些常用操作函数和属性
【9月更文挑战第7天】JS中的数组有哪些常用操作函数和属性
42 1
js基础笔记学习85-箭头函数得返回值
js基础笔记学习85-箭头函数得返回值
105 0
js基础笔记学习85-箭头函数得返回值
vant中遍历本地图片路径写法
vant中遍历本地图片路径写法
132 0
JS之字符串指定位置插入值
JS之字符串指定位置插入值
540 0
【100个 Unity小知识点】 | Unity中常用的几种单例写法
Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 。 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。 Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。
DoTween的基本用法
DoTween的基本用法 本文提供全流程,中文翻译。Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) ...
2060 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等