当前位置:网站首页>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
边栏推荐
- How to create Apple Developer personal account P8 certificate
- [original] all management without assessment is nonsense!
- [quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
- 过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
- 一大波开源小抄来袭
- Monthly observation of internet medical field in May 2022
- 保证接口数据安全的10种方案
- Vite path alias @ configuration
- Iterator and for of.. loop
- How to build your own super signature system (yunxiaoduo)?
猜你喜欢

20th anniversary of agile: a failed uprising

持续创作,还得靠它!

Vite path alias @ configuration

【微信小程序】Chapter(5):微信小程序基础API接口

航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆

Numpy --- basic learning notes

LeetCode3_ Longest substring without duplicate characters

Three. JS introductory learning notes 10:three JS grid
![[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)](/img/e9/9e32e38e12e1fa71732c52b8ee0ab0.png)
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)

Cocos uses custom material to display problems
随机推荐
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
Spin animation of Cocos performance optimization
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
C4D learning notes 3- animation - animation rendering process case
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
15. Using the text editing tool VIM
Summary of knowledge points of xlua hot update solution
Whole process analysis of unity3d rendering pipeline
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
nodejs package. JSON version number ^ and~
保证接口数据安全的10种方案
Cocos uses custom material to display problems
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Create lib Library in keil and use lib Library
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
Jacobo code coverage
Wireless sensor networks -- ZigBee and 6LoWPAN
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address