Unity基本脚本(平时测试时可以从这里复制)

代码1,获取当前游戏物体的transform的3个参数

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

public class Script1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject ob = this.gameObject;
        Debug.Log(string.Format("当前游戏物体的名称:{0}", ob.name));

        //获取transform组件
        Transform tr = this.gameObject.transform;
        Vector3 ps = this.gameObject.transform.position;

        Debug.Log(string.Format("x,y,z的值为:{0},{1},{2}",tr.position.x,tr.position.y,tr.position.z));

        Debug.Log(ps);//如(0.00, 0.00, 0.59)
        Debug.Log(ps.ToString("F3"));//如 (0.000, 0.000, 0.590),保留三位小数,否则打印的结果可能和检视视图上的值有一点差别

        //获取旋转角度
        Quaternion qu = this.gameObject.transform.rotation;
        Debug.Log(qu);//如 (0.00289, 0.00076, 0.00925, 0.99995)

        //获取缩放比例
        Vector3 sc = this.gameObject.transform.localScale;
        Debug.Log(sc);//如 (1.28, 3.07, 2.08)
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

代码2,获取世界坐标系和本地坐标系中的值

 

    /// <summary>
    /// 获取物体的世界坐标和本地坐标
    /// </summary>
    private void GetObjectPosition2()
    {
        //我在主场景中添加了一个Plane和Sphere,Plane是父物体,Shpere是子物体

        GameObject obj = this.gameObject;//获取当前游戏物体

        //世界坐标
        Vector3 ps = this.gameObject.transform.position;//获取当前游戏物体的位置
        Debug.Log("世界坐标系中的位置:" + ps);

        //本地坐标
        Vector3 ps1 = this.gameObject.transform.localPosition;
        Debug.Log("本地坐标系中的位置" + ps1);

        //简化写法(为了减轻书写负担,就提供了简化写法)
        Vector3 ps2 = this.transform.position;//世界坐标系中的位置(省略了gameObject)
        Vector3 ps3 = this.transform.localPosition;//本地坐标系中的位置(省略了gameObject)
    }

代码3,通过代码改变当前游戏物体的位置

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

public class Script3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //GameObject obj = this.gameObject;//获取当前游戏物体

        SetTransform();//设置游戏物体的位置,及缩放
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void SetTransform()
    {
        Vector3 posOrigin = this.transform.position;
        Debug.Log("初始位置为:" + posOrigin);

        this.transform.position = new Vector3(2, 2.5f, 2);//设置小球到新位置

        this.transform.localScale = new Vector3(2, 2.1f, 2);//放大小球
    }
}

  

 代码4,查看更新频率

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

public class Script3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Application.targetFrameRate = 60;//告诉引擎尽量以这个帧率来更新(约16.7ms更新一次)

        //GameObject obj = this.gameObject;//获取当前游戏物体

        SetTransform();//设置游戏物体的位置,及缩放
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("游戏启动后运行的时长:" + Time.time);
        Debug.Log("距上次更新的时间差:" + Time.deltaTime);
    }

    private void SetTransform()
    {
        Vector3 posOrigin = this.transform.position;
        Debug.Log("初始位置为:" + posOrigin);

        this.transform.position = new Vector3(2, 2.5f, 2);//设置小球到新位置

        this.transform.localScale = new Vector3(2, 2.1f, 2);//放大小球
    }
}

代码5,游戏物体沿X轴移动

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

public class Script3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log("游戏启动后运行的时长:" + Time.time);
        //Debug.Log("距上次更新的时间差:" + Time.deltaTime);

        MoveX();//沿X轴移动
    }

    /// <summary>
    /// //沿X轴移动(注意这个因为帧率不固定,所以速度并不是固定的)
    /// </summary>
    private void MoveX()
    {
        Vector3 pos = this.transform.localPosition;
        pos.x += 0.1f;
        this.transform.localPosition = pos;//移动

    }
}

代码6,让物体沿X轴匀速移动

 

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

public class Script3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log("游戏启动后运行的时长:" + Time.time);
        //Debug.Log("距上次更新的时间差:" + Time.deltaTime);

        MoveX();//沿X轴移动
    }

    /// <summary>
    /// //沿X轴移动(注意这个因为帧率不固定,所以速度并不是固定的)
    /// </summary>
    private void MoveX()
    {
        Vector3 pos = this.transform.localPosition;
        pos.x += 0.1f;
        this.transform.localPosition = pos;//移动

    }

    /// <summary>
    /// 沿X轴移动(匀速移动)
    /// </summary>
    private void MoveXOnSpeed()
    {
        float speed = 3;//每秒移动3米,即速度为3米/秒
        float distance = speed * Time.deltaTime;//速度*时间 == 应该移动到的位置

        Vector3 pos = this.transform.localPosition;
        pos.x += distance;
        this.transform.localPosition = pos;//这下就是匀速运动了,因为每次移动到的位置都是理论上应该运动到的位置


    }
}

代码7,使用transform.tranlate(dx,dy,dz)实现相对移动

    /// <summary>
    /// 沿X轴移动(匀速+相对)
    /// </summary>
    private void MoveXOnSpeed2()
    {
        float speed = 3;//每秒移动3米,即速度为3米/秒
        float distance = speed * Time.deltaTime;//速度*时间 == 应该移动到的位置

        transform.Translate(distance, 0, 0);
    }

  

 代码8,移动物体时,本地坐标系和世界坐标系的区别

    /// <summary>
    /// 沿X轴移动(本地坐标系和世界坐标系的区别)
    /// 注意,建模时,脸的模型要与物体的Z轴方向一致
    /// </summary>
    private void MoveXOnSpeed3()
    {
        float speed = 3;//每秒移动3米,即速度为3米/秒
        float distance = speed * Time.deltaTime;//速度*时间 == 应该移动到的位置

        //transform.Translate(distance, 0, 0,Space.World);//沿着世界坐标系的X轴移动,如果物体是一个小汽车,本地坐标系和世界坐标系不一致,汽车可能出现横着或歪着移动
        transform.Translate(distance, 0, 0, Space.Self);//沿着本地坐标系移动,这个相对会正常一些
    }

  

  

posted @ 2023-07-11 17:08  牛大胆V5  阅读(45)  评论(0)    收藏  举报