• Unity 物体移动


    一,首先创建一个Cube(这里我自己的是一个模型)

    二,创建3个空物体

    我取名point

    将空物体拖动到不同的地方。

    三,创建c# scrpit

    这里参考了:https://blog.csdn.net/czhenya/article/details/77412300

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class androidMove3 : MonoBehaviour
    {
        //空物体名称
        public Transform[] point;
        //移动速度
        public float speed = 3.0f;
        //循环变量
        int i = 0;
        //距离
        float des;
        void Start()
        {
    
        }
    
        private void Update()
        {
            //看向目标点
            //transform.LookAt(point[i].transform);
            LookAt2D(point[i].transform);
            //计算与目标点间的距离
            des = Vector3.Distance(this.transform.position, point[i].transform.position);
            //移向目标
            transform.position = Vector3.MoveTowards(this.transform.position, point[i].transform.position, Time.deltaTime * speed);
            //如果移动到当前目标点,就移动向下个目标
            if (des < 0.1f && i < point.Length - 1)
            {
                i++;
    
            }
        }
        void LookAt2D(Transform target)
        {
            Vector3 dir = target.position - transform.position;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.AngleAxis(90-angle, Vector3.up);
        }
    
    }

    四,绑定脚本

    下面是给cube对象,绑定我们的脚本。

    并且把3个空物体,拖到 Point下。

    (这也是为什么脚本中,point不用实例化的原因)

    五,运行

  • 相关阅读:
    1.1、html概述和基本结构
    几个常用的产品原型设计工具
    Redis 3. 与python交互
    Redis 2.2、主从配置
    Redis 2.1、发布订阅
    Redis 1.6、zset
    Redis 1.5、set
    Redis 1.4、list
    php实现手机拍照上传头像功能
    php获取文件mime类型Fileinfo等方法
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/14074825.html
Copyright © 2020-2023  润新知