当前位置: 首页 > news >正文

三、飞行和射击

目录

1.飞行的实现

2.限制玩家视角

3.射击的实现

4.附录


1.飞行的实现

(1)在Player预制体上挂载Configuration Joint组件,并修改其Y Drive属性

(2) 修改PlayerInput.cs和PlayerController.cs以实现飞行

  • PlayerInput.cs

添加以下属性 

[SerializeField] 
private float thrusterForce = 20f;
[SerializeField] 
private ConfigurableJoint joint

 在其Start方法中添加以下语句

joint = GetComponent<ConfigurableJoint>();

在其Update方法中添加以下语句 

Vector3 force = Vector3.zero;
if (Input.GetButton("Jump"))
{force = Vector3.up * thrusterForce;joint.yDrive = new JointDrive{positionSpring = 0f,positionDamper = 0f,maximumForce = 0f,};
}
else
{joint.yDrive = new JointDrive{positionSpring = 20f,positionDamper = 0f,maximumForce = 40f,};}
playerControllor.Thrust(force);
  • PlayerController.cs

添加以下属性 

private Vector3 thrusterForce = Vector3.zero;//向上的推力

 添加以下方法

public void Thrust(Vector3 _thrusterForce)
{thrusterForce = _thrusterForce;
}

 在其PerformMovement方法中添加以下语句

if (thrusterForce != Vector3.zero)
{rb.AddForce(thrusterForce);//作用Time.fixedDeltaTime秒:0.02秒thrusterForce = Vector3.zero;
}

2.限制玩家视角

 修改PlayerController.cs

添加以下属性

private float cameraRoatationTotal = 0f;//累计转了多少度
[SerializeField]
private float cameraRotationLimit = 85f;

对其PerformRotation方法进行一下修改

private void PerformRotation()
{if (yRotation != Vector3.zero){rb.transform.Rotate(yRotation);}if (xRotation != Vector3.zero){cam.transform.Rotate(xRotation);cameraRoatationTotal += xRotation.x;cameraRoatationTotal = Mathf.Clamp(cameraRoatationTotal, -cameraRotationLimit, +cameraRotationLimit);cam.transform.localEulerAngles = new Vector3(cameraRoatationTotal, 0f, 0f);}
}

3.射击的实现

(1) 创建并编写PlayerWeapon.cs,将其移动至Assets/Scripts/Player

using System;[Serializable]
public class PlayerWeapon
{public string name = "M16";public int damage = 10;public float range = 100f;
}

(2)在场景中创建空物体“GameManager”,创建并编写GameManager.cs并挂载至空物体“GameManager”

  • GameManager.cs
using System;
using UnityEngine;public class GameManager : MonoBehaviour
{private static string info;public static void UpdateInfo(String _info){info = _info;}private void OnGUI(){GUILayout.BeginArea(new Rect(200f,200f,200f,400f));GUILayout.BeginVertical();GUILayout.Label(info);GUILayout.EndVertical();GUILayout.EndArea();}
}

(3)创建并编写PlayerShooting.cs并将其挂载至Player预制体,将其移至Assets/Scripts/Player

using Unity.Netcode;
using UnityEngine;public class PlayerShooting : NetworkBehaviour
{[SerializeField]private PlayerWeapon weapon;[SerializeField] private LayerMask mask;private Camera cam;// Start is called before the first frame updatevoid Start(){cam = GetComponentInChildren<Camera>();}// Update is called once per framevoid Update(){if(Input.GetButton("Fire1")){Shoot();}}private void Shoot(){RaycastHit hit;if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit,weapon.range,mask)){ShootServerRpc(hit.collider.name,weapon.damage);}}[ServerRpc]private void ShootServerRpc(string hittedName,int damage){GameManager.UpdateInfo(transform.name+" hit "+hittedName);}
}

(4) 修改NetworkUI.cs,实现“点击按钮后按钮消失”

using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;public class NetworkManagerUI : MonoBehaviour
{[SerializeField] private Button hostBtn;[SerializeField] private Button serverBtn;[SerializeField] private Button clientBtn;// Start is called before the first frame updatevoid Start(){hostBtn.onClick.AddListener(() =>{NetworkManager.Singleton.StartHost();DestroyAllButtons();});serverBtn.onClick.AddListener(() =>{NetworkManager.Singleton.StartServer();DestroyAllButtons();});clientBtn.onClick.AddListener(() =>{NetworkManager.Singleton.StartClient();DestroyAllButtons();});}private void DestroyAllButtons(){Destroy(hostBtn.gameObject);Destroy(serverBtn.gameObject);Destroy(clientBtn.gameObject);}
}

4.附录 

(1)测试效果图

(按住空格起飞,朝向任意碰撞体按下鼠标左键,屏幕左上方出现命中提示) 

(2)部分工程文件完整代码

  • PlayerInput.cs
using UnityEngine;public class PlayerInput : MonoBehaviour
{[SerializeField]private float speed = 5f;[SerializeField] private float thrusterForce = 20f;[SerializeField] private PlayerController playerControllor;[SerializeField] private float lookSensitivity = 8f;[SerializeField] private ConfigurableJoint joint;// Start is called before the first frame updatevoid Start(){Cursor.lockState = CursorLockMode.Locked;joint = GetComponent<ConfigurableJoint>();}// Update is called once per framevoid Update(){float xMov = Input.GetAxisRaw("Horizontal");float yMov = Input.GetAxisRaw("Vertical");Vector3 velocity = (transform.right * xMov + transform.forward * yMov).normalized*speed;playerControllor.Move(velocity);float xMouse = Input.GetAxisRaw("Mouse X");float yMouse = Input.GetAxisRaw("Mouse Y");Vector3 yRotation = new Vector3(0f, xMouse, 0f)*lookSensitivity;Vector3 xRotation = new Vector3(-yMouse, 0f, 0f)*lookSensitivity;playerControllor.Rotate(yRotation,xRotation);Vector3 force = Vector3.zero;if (Input.GetButton("Jump")){force = Vector3.up * thrusterForce;joint.yDrive = new JointDrive{positionSpring = 0f,positionDamper = 0f,maximumForce = 0f,};}else{joint.yDrive = new JointDrive{positionSpring = 20f,positionDamper = 0f,maximumForce = 40f,};}playerControllor.Thrust(force);}
}
  • PlayerController.cs 
using UnityEngine;public class PlayerController : MonoBehaviour
{[SerializeField] private Rigidbody rb;[SerializeField] private Camera cam;private Vector3 velocity = Vector3.zero;//速度:每秒钟移动的距离private Vector3 yRotation=Vector3.zero;//旋转角色private Vector3 xRotation = Vector3.zero;//旋转视角private float cameraRoatationTotal = 0f;//累计转了多少度[SerializeField]private float cameraRotationLimit = 85f;private Vector3 thrusterForce = Vector3.zero;//向上的推力public void Move(Vector3 _velocity){velocity = _velocity;}public void Rotate(Vector3 _yRotation, Vector3 _xRotation){yRotation = _yRotation;xRotation = _xRotation;}public void Thrust(Vector3 _thrusterForce){thrusterForce = _thrusterForce;}private void PerformMovement(){if (velocity != Vector3.zero){rb.MovePosition(rb.position+velocity*Time.fixedDeltaTime);}if (thrusterForce != Vector3.zero){rb.AddForce(thrusterForce);//作用Time.fixedDeltaTime秒:0.02秒thrusterForce = Vector3.zero;}}private void PerformRotation(){if (yRotation != Vector3.zero){rb.transform.Rotate(yRotation);}if (xRotation != Vector3.zero){cam.transform.Rotate(xRotation);cameraRoatationTotal += xRotation.x;cameraRoatationTotal = Mathf.Clamp(cameraRoatationTotal, -cameraRotationLimit, +cameraRotationLimit);cam.transform.localEulerAngles = new Vector3(cameraRoatationTotal, 0f, 0f);}}private void FixedUpdate(){PerformMovement();PerformRotation();}
}

相关文章:

三、飞行和射击

目录 1.飞行的实现 2.限制玩家视角 3.射击的实现 4.附录 1.飞行的实现 &#xff08;1&#xff09;在Player预制体上挂载Configuration Joint组件&#xff0c;并修改其Y Drive属性 &#xff08;2&#xff09; 修改PlayerInput.cs和PlayerController.cs以实现飞行 PlayerIn…...

GitHub与GitHubDesktop的使用

1、介绍 见天来学习使用GitHub与GitHubDesktop。 学习前先来介绍一下什么是GitHub。 GitHub是一个基于Git的代码托管平台和开发者社区。它提供了一个Web界面&#xff0c;让开发者能够轻松地托管、共享和管理他们的软件项目。 在GitHub上&#xff0c;开发者可以创建自己的代…...

AIGC 微调的方法

AIGC 的微调方法可以分为以下步骤&#xff1a; 数据准备&#xff1a;收集尽可能多的数据&#xff0c;包括输入和输出数据&#xff0c;并将其划分为训练集、验证集和测试集。 模型选择&#xff1a;选择合适的模型结构&#xff0c;例如多层感知器&#xff08;MLP&#xff09;、卷…...

gcc编译webrtc x64

gcc使用Ubuntu系统已经有的gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 1、下载离线版webrtc&#xff08;也可以翻墙下载webrtc&#xff09; 百度云链接: 链接: https://pan.baidu.com/s/1oHVz9bxXlW3Q6uO996c5XA 提取码: ojbs 2、下载gn https://github.com/timnieder…...

uni-app 实现凸起的 tabbar 底部导航栏

效果图 在 pages.json 中设置隐藏自带的 tabbar 导航栏 "custom": true, // 开启自定义tabBar(不填每次原来的tabbar在重新加载时都回闪现) 新建一个 custom-tabbar.vue 自定义组件页面 custom-tabbar.vue <!-- 自定义底部导航栏 --> <template><v…...

中国1km土壤特征数据集(2010年)

简介&#xff1a; 中国1km土壤特征数据集&#xff08;2010&#xff09;是基于第二次全国土壤调查的中国1:1000000比例尺土壤图和8595个土壤剖面图&#xff0c;以及美国农业部&#xff08;USDA&#xff09;中国区域土地和气候模拟标准&#xff0c;开发了一个多层土壤粒度分布数…...

计算机网络笔记 第二章 物理层

2.1 物理层概述 物理层要实现的功能 物理层接口特性 机械特性 形状和尺寸引脚数目和排列固定和锁定装置 电气特性 信号电压的范围阻抗匹配的情况传输速率距离限制 功能特性 -规定接口电缆的各条信号线的作用 过程特性 规定在信号线上传输比特流的一组操作过程&#xff0…...

使用CreateProcess崩溃:处未处理的异常: 0xC0000005: 写入位置 0x00415652 时发生访问冲突

问题代码 if (!CreateProcess(NULL,L"pela.exe",NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)){return 0;}如果CreateProcess的第二个参数字符串是常量或者是储存在堆中的就会被写保护&#xff0c;崩溃。如果字符串定义到栈或者全局变量就不存在此问题了。 正确的…...

Java 华为真题-出租车计费

需求 程序员小明打了一辆出租车去上班。出于职业敏感&#xff0c;他注意到这辆出租车的计费表有点问题&#xff0c;总是偏大。 出租车司机解释说他不喜欢数字4&#xff0c;所以改装了计费表&#xff0c;任何数字位置遇到数字4就直接跳过&#xff0c;其余功能都正常。 比如&…...

开源layui前端框架 收款码生成系统源码 多合一收款码生成源码 带50多套UI模板

Layui前端的多合一收款码在线生成系统源码_附多套前端UI模板。 卡特三合一收款码生成系统源码&#xff0c;和收款啦采用一样的原理。 内部多达50多套模板&#xff0c;前端跟付款界面都特别好看。 识别收款码之后会自动加密&#xff0c;非常安全。 一样没有后台&#xff0c;一样…...

微服务moleculer01

1.官网地址&#xff1a; Moleculer - Progressive microservices framework for Node.js 2. github代码地址&#xff1a; GitHub - moleculerjs/moleculer: :rocket: Progressive microservices framework for Node.js Moleculer是基于Node.js的一款快速、多功能的微服务框…...

C++中将指针传递给函数

C中将指针传递给函数 指针是一种将内存空间传递给函数的有效方式&#xff0c;其中可包含函数完成其工作所需的数据&#xff0c;也可包含操作结果。将指针作为函数参数时&#xff0c;确保函数只能修改您希望它修改的参数很重要。例如&#xff0c;如果函数根据以指针方式传入的半…...

【51单片机编写占空比按秒渐亮与渐暗】2023-10-2

昨天刚在W10上安装CH340驱动&#xff0c;又下载到板子上LCD1602定时器时钟程序&#xff0c;为了调试&#xff0c;调用了一个LED观察控制蜂鸣器按秒响的变量&#xff0c;几经调试才发觉该开发板用的是有源蜂鸣器&#xff0c;不用IO取反操作&#xff0c;直接控制IO的高低电平即可…...

OCI 发布了容器运行时和镜像规范!

7 月 19 日是开放容器计划Open Container Initiative&#xff08;OCI&#xff09;的一个重要里程碑&#xff0c;OCI 发布了容器运行时和镜像规范的 1.0 版本&#xff0c;而 Docker 在这过去两年中一直充当着推动和引领的核心角色。 我们的目标是为社区、客户以及更广泛的容器行…...

C++学习笔记一: 变量和基本类型

本章讲解C内置的数据类型&#xff08;如&#xff1a;字符、整型、浮点数等&#xff09;和自定义数据类型的机制。下一章讲解C标准库里面定义的更加复杂的数据类型&#xff0c;比如可变长字符串和向量等。 1.基本内置类型 C内置的基本类型包括&#xff1a;算术类型和空类型。算…...

探索ClickHouse——同时支持导入导出功能的文件格式

在《探索ClickHouse——安装和测试》中&#xff0c;我们使用clickhouse直接从文件中读取数据。clickhouse支持多种格式文件的导入导出&#xff0c;本节我们对此进行分类介绍。 按常见格式区分 JSON 原始的JSON格式只支持导入&#xff0c;不支持导入。同时支持导入和导出的是…...

Scipy库提供了多种正态性检验和假设检验方法

Scipy库提供了多种正态性检验和假设检验方法。以下是一些常用的检验方法的列表&#xff1a; 正态性检验方法&#xff1a; Shapiro-Wilk检验&#xff1a;scipy.stats.shapiroAnderson-Darling检验&#xff1a;scipy.stats.andersonKolmogorov-Smirnov检验&#xff1a;scipy.st…...

去雨去雪去雾算法之本地与服务器的TensorBoard使用教程

在进行去雨去雾去雪算法实验时&#xff0c;需要注意几个参数设置&#xff0c;num_workers只能设置为0&#xff0c;否则会报各种稀奇古怪的错误。 本地使用TensorBoard 此外&#xff0c;发现生成的文件是events.out.tfevents格式的&#xff0c;查询了一番得知该文件是通过Tens…...

【小沐学前端】Node.js实现基于Protobuf协议的WebSocket通信

文章目录 1、简介1.1 Node1.2 WebSocket1.3 Protobuf 2、安装2.1 Node2.2 WebSocket2.2.1 nodejs-websocket2.2.2 ws 2.3 Protobuf 3、代码测试3.1 例子1&#xff1a;websocket&#xff08;html&#xff09;3.1.1 客户端&#xff1a;yxy_wsclient1.html3.1.2 客户端&#xff1a…...

MySQL学习笔记24

MySQL的物理备份&#xff1a; xtrabackup备份介绍&#xff1a; xtrabackup优缺点&#xff1a; 优点&#xff1a; 1、备份过程快速、可靠&#xff08;因为是物理备份&#xff09;&#xff1b;直接拷贝物理文件。 2、支持增量备份&#xff0c;更为灵活&#xff1b; 3、备份…...

【入坑系列】TiDB 强制索引在不同库下不生效问题

文章目录 背景SQL 优化情况线上SQL运行情况分析怀疑1:执行计划绑定问题?尝试:SHOW WARNINGS 查看警告探索 TiDB 的 USE_INDEX 写法Hint 不生效问题排查解决参考背景 项目中使用 TiDB 数据库,并对 SQL 进行优化了,添加了强制索引。 UAT 环境已经生效,但 PROD 环境强制索…...

Qt Widget类解析与代码注释

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码&#xff0c;写上注释 当然可以&#xff01;这段代码是 Qt …...

UDP(Echoserver)

网络命令 Ping 命令 检测网络是否连通 使用方法: ping -c 次数 网址ping -c 3 www.baidu.comnetstat 命令 netstat 是一个用来查看网络状态的重要工具. 语法&#xff1a;netstat [选项] 功能&#xff1a;查看网络状态 常用选项&#xff1a; n 拒绝显示别名&#…...

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1)&#xff1a;从基础到实战的深度解析-CSDN博客&#xff0c;但实际面试中&#xff0c;企业更关注候选人对复杂场景的应对能力&#xff08;如多设备并发扫描、低功耗与高发现率的平衡&#xff09;和前沿技术的…...

BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践

6月5日&#xff0c;2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席&#xff0c;并作《智能体在安全领域的应用实践》主题演讲&#xff0c;分享了在智能体在安全领域的突破性实践。他指出&#xff0c;百度通过将安全能力…...

HDFS分布式存储 zookeeper

hadoop介绍 狭义上hadoop是指apache的一款开源软件 用java语言实现开源框架&#xff0c;允许使用简单的变成模型跨计算机对大型集群进行分布式处理&#xff08;1.海量的数据存储 2.海量数据的计算&#xff09;Hadoop核心组件 hdfs&#xff08;分布式文件存储系统&#xff09;&a…...

基于Java Swing的电子通讯录设计与实现:附系统托盘功能代码详解

JAVASQL电子通讯录带系统托盘 一、系统概述 本电子通讯录系统采用Java Swing开发桌面应用&#xff0c;结合SQLite数据库实现联系人管理功能&#xff0c;并集成系统托盘功能提升用户体验。系统支持联系人的增删改查、分组管理、搜索过滤等功能&#xff0c;同时可以最小化到系统…...

接口自动化测试:HttpRunner基础

相关文档 HttpRunner V3.x中文文档 HttpRunner 用户指南 使用HttpRunner 3.x实现接口自动化测试 HttpRunner介绍 HttpRunner 是一个开源的 API 测试工具&#xff0c;支持 HTTP(S)/HTTP2/WebSocket/RPC 等网络协议&#xff0c;涵盖接口测试、性能测试、数字体验监测等测试类型…...

FFmpeg:Windows系统小白安装及其使用

一、安装 1.访问官网 Download FFmpeg 2.点击版本目录 3.选择版本点击安装 注意这里选择的是【release buids】&#xff0c;注意左上角标题 例如我安装在目录 F:\FFmpeg 4.解压 5.添加环境变量 把你解压后的bin目录&#xff08;即exe所在文件夹&#xff09;加入系统变量…...

从面试角度回答Android中ContentProvider启动原理

Android中ContentProvider原理的面试角度解析&#xff0c;分为​​已启动​​和​​未启动​​两种场景&#xff1a; 一、ContentProvider已启动的情况 1. ​​核心流程​​ ​​触发条件​​&#xff1a;当其他组件&#xff08;如Activity、Service&#xff09;通过ContentR…...