unity随笔日记2

发布时间:2022-06-28 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了unity随笔日记2脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、Unity 指定区域随机实例化预制体PRefab 代码

unity随笔日记2

unity随笔日记2

using UnITyEngine;

public class NewBehaviourScript : MonoBehaviour {
    public GameObject prefab;
    void Update()
    {
        //位置
        float x = Random.Range(-10, 10);
        float y = Random.Range(-10, 10);
        float z = Random.Range(-10, 10);
        Vector3 pos = new Vector3(x,y,z);
        //实例化
        Instantiate(prefab,pos,Quaternion.identity);

    }
}
View Code

效果:脚本绑定到到物件,把任意prefab拖到脚本中的Prefab的参数上,运行,每一帧都在空间内随机生成一个prefab的实例。

参考:https://www.cnblogs.COM/36bian/p/7571607.htML

 

 

二、unity-实现摄像机跟随物体(Vector3.SmoothDamp)

unity随笔日记2

unity随笔日记2

using UnityEngine;
using System.Collections;

public class FllowTarget : MonoBehaviour {

  public Transform target;  //摄像机要跟随的人物
  public float smoothTime = 0.01f; //摄像机平滑移动的时间
  private Vector3 cameraVelocity = Vector3.zero;
  private Camera mainCamera; //主摄像机(有时候会在工程中有多个摄像机,但是只能有一个主摄像机吧)

  void Awake () 
  { 
   mainCamera = Camera.main;
  }

  void Update()
  {
    transform.position = Vector3.SmoothDamp(transform.position, target.position + new Vector3(0, 0, -5), ref cameraVelocity, smoothTime);
  }

}
View Code

Vector3.SmoothDamp:

Vector3.SmoothDamp 平滑阻尼

static function SmoothDamp (current : Vector3, target : Vector3, ref currentVelocity : Mathf.Infinity, deltaTime : float = Time.deltaTime) : Vector3

Parameters参数

current
当前的位置
target
我们试图接近的位置
currentVelocity
当前速度,这个值由你每次调用这个函数时被修改
smoothTime
到达目标的大约时间,较小的值将快速到达目标
maxSPEed
选择允许你限制的最大速度
deltaTime
自上次调用这个函数的时间。默认为 Time.deltaTime

Description描述

随着时间的推移,逐渐改变一个向量朝向预期的目标。向量由一些像弹簧阻尼器函数平滑,这将永远不会超过。最常见的用途是平滑跟随相机。

参考:https://www.it610.com/article/1280425870741225472.htm

 

 

三、Unity计算两点之间的距离

向量的长度是用勾股定理计算出来,计算机计算两次方和开根的运算量比加减法要费时的多。

所以求两个对象的距离最常用的是比较两点距离的平方

unity随笔日记2

unity随笔日记2

     float dis = (transform.position - player.position).sqrMagnitude
View Code

参考:https://bLOG.csdn.net/jbl20078/article/details/77703290

 

 

四、unity绕定点循环(RotateAround)

unity随笔日记2

unity随笔日记2

        RotateAround(GameObject.Find("Cave").transform.position, transform.right, Time.deltaTime * 10);//世界坐标。

        RotateAround(GameObject.Find("Cave").transform.position, new Vector(0,0,1), Time.deltaTime * 10);//自身的朝向。

        RotateAround(GameObject.Find("Cave").transform.position, GameObject.Find("Cave").transform.right, Time.deltaTime * 10);//围绕的点的朝向。
View Code

 参考:https://www.cnblogs.com/JMarshall/p/10749595.html

 

 

五、物体运动速度(rigibody.velocity)

unity随笔日记2

unity随笔日记2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Randomcoloc :MonoBehaviour{
    public Rigidbody rig:
    //Use this for initialization
    void Start(){
        rig = Getcomponent<Rigidbody>();
    }

    //Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonUp(0))
            get_speed();
    }
    public void get_speed(){
        float temp_speed = rig.velocity.magnitude;
        Debug.Log(temp_speed);
    }
}
View Code

 

脚本宝典总结

以上是脚本宝典为你收集整理的unity随笔日记2全部内容,希望文章能够帮你解决unity随笔日记2所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。