当前位置:网站首页>Unity3D_ Class fishing project, bullet rebound effect is achieved
Unity3D_ Class fishing project, bullet rebound effect is achieved
2022-07-07 15:52:00 【Le_ Sam】
The bullet rebound diagram is as follows :
1. Both the wall and the bullet are collision bodies
2. Prerequisite : You need to know the direction vector of the bullet , Wall direction vector
3. Simple calculation process , Yellow is the bullet direction vector , Calculate the included angle with the wall , Calculate the orange direction vector according to the incident angle equal to the reflection angle . At this time, only the direction of the bullet changed , Next, calculate the offset of the position , According to the angle with the wall and the size of the bullet itself, the trigonometric function sin/cos Calculate the offset , The final effect is like the red arrow .
Code :
-- The bullet bounced ,ColliderObj: Collision wall total 4 Noodles .
-- self.bulletObjLength Estimate the bullet length , self.Dir Is the current direction vector of the bullet , self.o_Transform For example bullets
function BCAmmoBase:ReflectBullet(ColliderObj)
-- inNormal: Wall direction vector , Used to calculate the reflection angle , Is the normal
-- XOrZ: Determine whether the bullet hit the horizontal wall or the vertical wall
-- iden: The sign of the wall where the bullet hit , Used to record the wall of the last collision
local inNormal, XOrZ, iden = nil
if tostring(ColliderObj.transform.name) == "ForwardWall" then
inNormal = Vector3(0, 0, -1)
XOrZ = true
iden = "F"
elseif tostring(ColliderObj.transform.name) == "BackWall" then
inNormal = Vector3(0, 0, 1)
XOrZ = true
iden = "B"
elseif tostring(ColliderObj.transform.name) == "LeftWall" then
inNormal = Vector3(1, 0, 0.1)
XOrZ = false
iden = "L"
elseif tostring(ColliderObj.transform.name) == "RightWall" then
inNormal = Vector3(-1, 0, 0.1)
XOrZ = false
iden = "R"
end
if inNormal == nil or iden == nil then
-- log("inNormal == nil or iden == nil ")
return
end
-- To avoid bullets flying out of the wall , It is forbidden to bounce multiple times on the same wall
if iden == self.collideLastIden then
-- log("iden == self.collideLastIden ")
return
end
self.collideLastIden = iden
self.collideInNormal = inNormal
self.collideXOrZ = XOrZ
local reflexDir = Vector3.Reflect(self.Dir, self.collideInNormal)
local dirAngle = 90 - Vector3.Angle(reflexDir, self.collideInNormal)
local dic = math.sin(math.rad(dirAngle)) * self.bulletObjLength
-- log(tostring(reflexDir) .. " Reflection vector ")
-- log(tostring(dirAngle) .. " The angle between the reflection vector and the position correction ")
-- log(tostring(dic) .. " Position correction ")
-- Position correction
local pos = self.o_Transform.position
if self.collideXOrZ then
self.o_Transform.position = reflexDir.x >= 0 and pos + Vector3(dic, 0, 0) or pos - Vector3(dic, 0, 0)
else
self.o_Transform.position = reflexDir.z >= 0 and pos + Vector3(0, 0, dic) or pos - Vector3(0, 0, dic)
end
-- Direction correction
local dir = Vector3.Normalize(reflexDir)
self.Dir = Vector3(dir.x, 0, dir.z) -- Y The shaft is fixed to 0, Avoid tilting bullets up or down
self.o_Transform.rotation = Quaternion.FromToRotation(Vector3.forward, self.Dir)
end
边栏推荐
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
- Whole process analysis of unity3d rendering pipeline
- Whether runnable can be interrupted
- Three. JS introductory learning notes 13: animation learning
- ./ Functions of configure, make and make install
- [quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
- LeetCode3_ Longest substring without duplicate characters
- HW primary flow monitoring, what should we do
- Asynchronous application of generator function
猜你喜欢
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
10 schemes to ensure interface data security
Tkinter after how to refresh data and cancel refreshing
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
[wechat applet] Chapter (5): basic API interface of wechat applet
Steps to create P8 certificate and warehousing account
Cocos uses custom material to display problems
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
A wave of open source notebooks is coming
随机推荐
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
AE learning 02: timeline
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Learn good-looking custom scroll bars in 1 minute
Do not use memset to clear floating-point numbers
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
The difference between full-time graduate students and part-time graduate students!
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
Use of SVN
Monthly observation of internet medical field in May 2022
Clang compile link ffmpeg FAQ
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Cut ffmpeg as needed, and use emscripten to compile and run
XMIND frame drawing tool
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
./ Functions of configure, make and make install
HPDC smart base Talent Development Summit essay