7K7K.unity

news/2024/10/28 20:17:06/

cube克隆加重力

Cube旋转需要旋转类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Exercise : MonoBehaviour
{
    public Texture texture = null;
    private GameObject CubeGo;
    public void ButtonCubeBorn()    //Cube生成
    {
        CubeGo = GameObject.CreatePrimitive(PrimitiveType.Cube);
        CubeGo.transform.position = Vector3.zero;
        CubeGo.tag = "Player";
    }
    public void ButtonColor()      //添加颜色
    {
        if (CubeGo != null)
        {
            CubeGo.GetComponent<Renderer>().material.color = Color.red;
            CubeGo.GetComponent<Renderer>().material.mainTexture = null;
        }
    }
    public void ButtonMaterial()    //添加材质
    {
        if (CubeGo != null)
        {
            CubeGo.GetComponent<Renderer>().material = null;
            CubeGo.GetComponent<Renderer>().material.mainTexture = texture;
        }
    }
    public void ButtonRotate()   //旋转Cube
    {
        if (CubeGo != null)
        {
            CubeGo.AddComponent<Rotate>();
        }
    }
    public void ButttonClone()   //Cube克隆
    {
        for (int i = 0; i < 10; i++)
        {
            CubeGo = (GameObject)Instantiate(CubeGo);
            CubeGo.transform.position = new Vector3(CubeGo.transform.position.x + 2, CubeGo.transform.position.y, CubeGo.transform.position.z);
        }
    }
    public void ButtonAddRigi()         //添加重力
    {
        GameObject[] goes = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < goes.Length; i++)
        {
            goes[i].AddComponent<Rigidbody>();
        }
    }
    public void ButtonRigiDistroy()     //重力消失
    {
        if (CubeGo != null)
        {
            Destroy(CubeGo.GetComponent<Rigidbody>());
            CubeGo.transform.position = Vector3.zero;
        }
    }
    public void ButtonCubeDistory()       //Cube消失
    {
        if (CubeGo != null)
        {
            GameObject[] goes = GameObject.FindGameObjectsWithTag("Player");
            foreach (GameObject item in goes)
            {
                Destroy(item);
            }
        }
    }
    public void ButtonExit()   //退出游戏
    {
        Application.Quit();
    }
   
}

旋转类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour   //旋转类
{
    void Update()
    {
        this.transform.Rotate(Vector3.up, 1f);
    }
}
 

场景切换

using UnityEngine.SceneManagement;

 public void LoadScene()
    {
        SceneManager.LoadScene("Scene2");
    }

控制摄像机或物体移动

public class Move : MonoBehaviour
{
    public float Speed = 3;

    void Update()
    {
        this.transform.Translate(Input.GetAxis("Vertical") * Vector3.forward*Speed * Time.deltaTime);
        this.transform.Translate(Input.GetAxis("Horizontal") * Vector3.right*Speed * Time.deltaTime);
    }
}

控制物体移动:

CharacterController cc;

void Start()
    {
        cc = this.GetComponent<CharacterController>();
    }
void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            cc.Move(Vector3.forward * Time.deltaTime*10);
        }
        if (Input.GetKey(KeyCode.S))
        {
            cc.Move(Vector3.back * Time.deltaTime * 10);
        }
        if (Input.GetKey(KeyCode.A))
        {
            cc.Move(Vector3.left * Time.deltaTime * 10);
        }
        if (Input.GetKey(KeyCode.D))
        {
            cc.Move(Vector3.right * Time.deltaTime * 10);
        }
    }

碰撞掉血

胶囊体:胶囊体挂胶囊 画布挂UI UIplayer挂带脚本的胶囊体对象 墙体标签为wall
public class Capsule : MonoBehaviour
{
    public int maxHP=10;   //最大血量
    public int currentHP;   //当前血量
    public float Speed = 10;
    void Start()
    {
        currentHP = maxHP;    //初始化当前血量为最大
    }
    void Update() //移动
    {
        this.transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Speed * Time.deltaTime);
        this.transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * Speed * Time.deltaTime);
    }
    private void OnCollisionEnter(Collision collision)  //碰撞器
    {
        if (collision.transform.tag=="Wall")  //判断碰撞物的标签是wall
        {
             Wound(); //执行掉血
        }
    }
    public void Wound()
    {
        if (currentHP > 0)
        {
            currentHP--; //掉血
        }
    }
}

slider值类:

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

public class UI : MonoBehaviour
{
    public Capsule player;
    public Slider sliderHP;
    void Update()
    {
        sliderHP.value = player.currentHP;
    }
}

射击围墙

预制体Cube和预制体子弹Bullet  (刚体)、空对象

public class Shot : MonoBehaviour
{
    public float Speed = 3;
    public GameObject Bullet;

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            GameObject b = GameObject.Instantiate(Bullet, transform.position, transform.rotation);
            Rigidbody rib = b.GetComponent<Rigidbody>();
            rib.velocity = transform.forward * Speed;
        }
    }
}
 

摄像机跟随

public class Camera : MonoBehaviour
{
    public Transform follow;
    public float distanceAway = 5.0f;
    public float distanceUp = 2.0f;
    public float smooth = 1.0f;
    private Vector3 targePosition;
 
    void LateUpdate()
    {
        targePosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
        transform.position = Vector3.Lerp(transform.position, targePosition, Time.deltaTime * smooth);
        transform.LookAt(follow);
    }
}

退出游戏

public class Exit : MonoBehaviour
{
    // Start is called before the first frame update
    public void OnExitGame()
    {
#if UNITY_EDITOR//在编辑器模式退出
        UnityEditor.EditorApplication.isPlaying = false;
#else//发布后退出
        Application.Quit();
#endif
    }
}

小球导航

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

public class Move : MonoBehaviour
{
    public Transform target;
    NavMeshAgent nav;
    void Start()
    {
        nav = this.gameObject.GetComponent<NavMeshAgent>();
    }
    void Update()
    {
       if(nav&&target)
        {
            nav.SetDestination(target.position);
        }
    }
}

寻路

    寻路:
    public GameObject _dest;
    UnityEngine.AI.NavMeshAgent _agent;

    void Start()
    {
        _agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    void Update()
    {
        _agent.SetDestination(_dest.transform.position);
    }

plane开关:
using UnityEngine.AI;
public class 开关 : MonoBehaviour
{
    NavMeshObstacle _obs;
    void Start()
    {
        _obs = this.GetComponent<NavMeshObstacle>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            _obs.enabled = false;
            this.GetComponent<Renderer>().material.color = Color.green;
        }
        if (Input.GetMouseButtonUp(0))
        {
            _obs.enabled = true;
            this.GetComponent<Renderer>().material.color = Color.red;
        }
    }
}

生成cube加射线加移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class 围墙 : MonoBehaviour
{
    public GameObject cubeOriginal;
    public Image img;
    public CharacterController cc;
    public Transform m_player;
    void Start()
    {
        cc = this.GetComponent<CharacterController>();
        for (int j=0;j<=5;j++)
        {
            for(int i=0;i<=10;i++)
            {
                GameObject cubeClone = Instantiate(cubeOriginal);
                cubeClone.transform.position = new Vector3(i, j, 0);
            }
        }
    }

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray,out hit))
        {
            Debug.DrawLine(ray.origin, hit.point);
            img.transform.position = Input.mousePosition;
        }
        if (Input.GetMouseButtonDown(0))
        {
            GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            bullet.AddComponent<Rigidbody>();
            bullet.transform.position = Camera.main.transform.position;
            bullet.GetComponent<Rigidbody>().AddForce(600*(hit.point-bullet.transform.position));
            Destroy(bullet,3);
        }
        if (Input.GetKey(KeyCode.W))
        {
            cc.Move(Vector3.forward * Time.deltaTime * 10);
        }
        if (Input.GetKey(KeyCode.S))
        {
            cc.Move(Vector3.back * Time.deltaTime * 10);
        }
        if (Input.GetKey(KeyCode.A))
        {
            cc.Move(Vector3.left * Time.deltaTime * 10);
        }
        if (Input.GetKey(KeyCode.D))
        {
            cc.Move(Vector3.right * Time.deltaTime * 10);
        }
        Debug.DrawRay(m_player.position, m_player.forward, Color.red);
    }
    public void LoadScene()
    {
        SceneManager.LoadScene("Scene1");
    }

}
 


http://www.ppmy.cn/news/551481.html

相关文章

SQL Server 无法将“WIN-C6BGU7K6VUR”配置为分发服务器

1、SQL Server2008数据库在进行开启复制分发的配置时&#xff0c;出现了报错&#xff1a; 正在配置... - 正在配置分发服务器 (错误) 消息 SQL Server 无法将“WIN-C6BGU7K6VUR”配置为分发服务器。 (Microsoft.SqlServer.ConnectionInfo) ------------------------------ 其…

罗斯蒙特751AM7K6BCBT现场信号指示器

罗斯蒙特751AM7K6BCBT现场信号指示器 使用专为恶劣工业环境设计的 Rosemount 751 现场信号指示器访问重要过程数据。此款现场信号指示器与任何测量输入变量&#xff08;例如压力、流量、液位或温度&#xff09;的两线制变送器一起使用。此装置适用于难以查看内置仪表的安装情况…

05.内存管理:动态申请和释放内存

动态分配内存&#xff0c;进行内存管理 参考: 伙伴算法原理简介 linux 0.11源码 本文主要针对Linux0.11的malloc和free进行分析。是一种类似伙伴系统的内存管理方法&#xff0c;不过伙伴系统的内存通常是申请大于一页的内存&#xff0c;但是在该内核版本的内存管理&#xff0c…

Linux2.基础指令(下)

1.uname -r :输出Linux内核版本信息。 2.linux2.6.*内核默认支持的文件系统有ext3,ext2,ext4,xfs&#xff0c;不支持ufs。 3.linux查看CPU占用的命令:top。 4.题目 5.题目 6.题目 7.重定向 echo "字符串1" :在屏幕上打印字符串1。 echo "字符串1" &g…

git修改默认主分支main为master和设置git默认创建的项目默认分支都为master

文章目录 前言一、设置新建仓库默认分支为master1.点击GitHub右上角的头像2. 选中settings&#xff08;设置&#xff09;3.点击Repositories&#xff08;存储库&#xff09;4.更改main为master后点击update 二、设置已建仓库的默认分支为master1.找到你要改的项目点击settings&…

java GB28181视频监控 大华 海康摄像机国标对接源码源代码程序

java GB28181视频监控 大华 海康摄像机国标对接源码源代码程序 WEB VIDEO PLATFORM是一个基于GB28181-2016标准实现的网络视频平台&#xff0c;负责实现核心信令与设备管理后台部分&#xff0c;支持NAT穿透&#xff0c;支持海康、大华、宇视等品牌的IPC、NVR、DVR接入。支持国标…

Da黄蜂vep云课堂6.05录屏截屏提取为mp4教程

最近下载了一套视频&#xff0c;视频是vep加密的&#xff0c;只有通过 大 黄 蜂 云课堂才能播放。 经过了解知道vep的加密视频是重编码加密&#xff0c;所以几乎很难破解。 所以给大家找了一款 大 黄蜂云课堂6.05内部版&#xff0c;可直接翻录为mp4格式。只要本地能播放就可以…

【双目相机】python使用双目摄像头录像、调用摄像头、调用视频

1.调用摄像头 #读取摄像头 import cv2 capcv2.VideoCapture(0) #capcv2.VideoCapture(output.avi) if not cap.isOpened():print("Cannot open camera")exit() while True:# 逐帧捕获ret,framecap.read()# 如果正确读取帧&#xff0c;ret为Trueif not ret:break# 显…