当前位置:网站首页>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
边栏推荐
- Postman generate timestamp, future timestamp
- JS array foreach source code parsing
- [original] all management without assessment is nonsense!
- Numpy --- basic learning notes
- Simple understanding and application of TS generics
- 2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
- [wechat applet] Chapter (5): basic API interface of wechat applet
- A JS script can be directly put into the browser to perform operations
- Align individual elements to the right under flex layout
- HPDC smart base Talent Development Summit essay
猜你喜欢

SPI master rx time out中断

Async and await

Vite path alias @ configuration

【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)

LeetCode3_ Longest substring without duplicate characters

Three. JS introductory learning notes 04: external model import - no material obj model

AE learning 01: AE complete project summary

10 schemes to ensure interface data security
![Super signature principle (fully automated super signature) [Yun Xiaoduo]](/img/b8/5bafbada054b335568e64c7e1ac6bb.jpg)
Super signature principle (fully automated super signature) [Yun Xiaoduo]

Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
随机推荐
How to build your own super signature system (yunxiaoduo)?
Ue4/ue5 multi thread development attachment plug-in download address
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
C4D learning notes 3- animation - animation rendering process case
Syntaxhighlight highlights the right scroll bar
Three. JS introduction learning notes 12: the model moves along any trajectory line
OpenGL's distinction and understanding of VAO, VBO and EBO
TS typescript type declaration special declaration field number is handled when the key key
Function: JS Click to copy content function
Asynchronous application of generator function
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
Three. JS introductory learning notes 10:three JS grid
The rebound problem of using Scrollview in cocos Creator
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Configure mongodb database in window environment
nodejs package. JSON version number ^ and~
TS as a general cache method
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
Numpy -- data cleaning
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code