找回密码
 立即注册
查看: 219|回复: 0

unity基础脚本代码总结

[复制链接]
发表于 2022-9-6 07:55 | 显示全部楼层 |阅读模式
1.控制物体移动

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

public class sport : MonoBehaviour
{
    public Rigidbody cube;//创建刚体对象
    // Start is called before the first frame update
    void Start()
    {
        cube = GetComponent<Rigidbody>();//Cube得到刚体组件(方法)
    }

    // Update is called once per frame
    void Update()
    {
        float a, b, c;
        //内置方法可在Edit- projectsettings -Axis中查看
        a = Input.GetAxis("Horizontal");//按ws或者上下方向可以向前前后移动
        b = Input.GetAxis("Vertical");  //按ad或者左右方向可以向左向右移动
        c = Input.GetAxisRaw("Jump");   //按一直空格可以飞行跳跃
        cube.AddForce(new Vector3(a, c, b) *10 );
        // Addforce也就是给cube添加一个力 Vector是它的三维方向轴
        //所以按wsad和空格 可以对物体在不同的方向产生不同的力从而达到运动的作用
        //乘上10是为了加快它的速度


    }
}
2.视野跟随

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

public class followsight : MonoBehaviour
{
    public Transform  cube; //创建跟随对象
    private Vector3 offset;        //偏移视角
    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - cube.position;
        //记录初始相机到物体的三维差
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = offset + cube.position;
        //此时相机的位置就一直跟着cube的位置变化而变化
        //那么此时物体和相机就成了形影不离的整体
    }
}
3.预制体旋转

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

public class rotate : MonoBehaviour
{
    public float speed=5;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Vector3.forward * speed);
        //rotate是旋转, 那么forward是向前当然可以选其他方向,然后可以看到我定义了一个speed,
        //那么这个speed也就是速度的意思可以在Inspector面板中的script组件中修改speed的值
    }
}
4.触发器吃食物

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

public class chufa : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    // private void OnCollisionEnter(Collision collision)//这个是碰撞器方法,enter意味着发生碰撞接触时会发生什么
    //private void OnCollisionEnter(Collision collision)//这个是停留时会发生什么
    //private void OnCollisionExit(Collision collision)//那么这个就是离开时会发生什么
    //此时我们是接触器如下
    private void OnTriggerEnter(Collider other)//接触时
    {
        if(other.gameObject .tag =="food")//当接触的物体它的trandform组件中的tag标签是food时执行
        {
            Destroy(other.gameObject );  //此时让食物物体对象消失
        }
    }


}
5.激活隐藏物件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//涉及UI时需要加上这个命名空间
public class chufa : MonoBehaviour
{

    // Start is called before the first frame update
    public float socrce = 0; //定义一个分数值让他初始值为0
    public GameObject ui; //创建要激活的对象gameobjet类型
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    // private void OnCollisionEnter(Collision collision)//这个是碰撞器方法,enter意味着发生碰撞接触时会发生什么
    //private void OnCollisionEnter(Collision collision)//这个是停留时会发生什么
    //private void OnCollisionExit(Collision collision)//那么这个就是离开时会发生什么
    //此时我们是接触器如下
    private void OnTriggerEnter(Collider other)//接触时
    {
        if (other.gameObject.tag == "food")//当接触的物体它的trandform组件中的tag标签是food时执行
        {
            Destroy(other.gameObject);  //此时让食物物体对象消失
            socrce++; //吃到一个食物得到一分
        }
        if(socrce == 4.0F) //F是float的后缀当吃到四个食物时就激活胜利信息
        {
            ui.SetActive(true);//setActive就是激活true就是实现按钮
        }

    }


}
6.发射预制体

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

public class shoot2 : MonoBehaviour
{
    public GameObject phere; //定义一个游戏物体对象
    // Start is called before the first frame update
    public float speed;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input .GetMouseButtonDown(0))//当单击鼠标左键时实现
        {
            GameObject timeobjects = GameObject.Instantiate(phere, transform.position, transform.rotation);
            //此时每次实例化物体,phere ,得到它的位置信息,角度信息
            Rigidbody timeobjectson = phere.GetComponent<Rigidbody>();
            //实时定义一个刚体对象timeobject让他得到游戏物体的刚体组件
            timeobjectson.velocity = Vector3.forward * speed;
            //引用内置速度方法                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        }
    }
}
7.非刚体组件物体移动

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

public class shoot : MonoBehaviour
{

   // public Rigidbody camare;
    public float speed =0 ;
    // Start is called before the first frame update
    void Start()
    {
      //   camare = GetComponent <Rigidbody >();
        
    }

    // Update is called once per frame
    void Update()
    {
        float a, b;
        a = Input.GetAxis("Horizontal");
        b = Input.GetAxis("Vertical");
        transform.Translate(new Vector3(a, b, 0) * Time.deltaTime * speed);
        //因为Update每秒50贞,Time.daltatime为50分之一,所以按照了每秒一米的速度移动
   
    }

}
8.AI自动导航和动画切换参数的添加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class navmesh : MonoBehaviour
{
    // Start is called before the first frame update
    public NavMeshAgent boss;//创建一个网格导航对象Boss
    public Animator bboss;      //用于人物动画切换的参数
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //利用射线来判断指引位置
       if(Input .GetMouseButtonDown (0))// 当按下鼠标右键时运行
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //相当于发射光线ray 让其获得由当前相机屏幕前鼠标点击处转化来的射线点位置
            RaycastHit store;  
            //创建Raycastthit类型的对象,为下面让其保存射线碰撞的位置的信息做铺垫
            //RaycastHit 意思为光线投射碰撞
            //Raycast 则是光线投射
        if(Physics .Raycast(ray ,out store ) )
            //此时调用内置的方法,按输出传递参数Out,返回值为两个,一个给了store另一个返回ture
            {
                boss.SetDestination(store.point);
                //此时游戏物体boss接下来要到达的位置已经生成
                bboss.SetFloat("speed", boss.velocity.magnitude);
                //给动画对象设置一个float类型的参数,该参数的名称为“speed”,值为网格导航对象boss的速度的值
            }
        }
    }
}
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-30 09:01 , Processed in 0.104617 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表