当前位置:网站首页>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
边栏推荐
- [quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
- Configure mongodb database in window environment
- Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
- UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- Annexb and avcc are two methods of data segmentation in decoding
- A wave of open source notebooks is coming
- 一大波开源小抄来袭
- [Lanzhou University] information sharing of postgraduate entrance examination and re examination
- Mesh merging under ue4/ue5 runtime
猜你喜欢

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan

Keil5 does not support online simulation of STM32 F0 series

numpy---基础学习笔记
Introduction of mongod management database method

深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)

Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages

Vertex shader to slice shader procedure, varying variable

Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)

航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
随机推荐
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
Do not use memset to clear floating-point numbers
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
The rebound problem of using Scrollview in cocos Creator
Getting started with webgl (1)
Learn good-looking custom scroll bars in 1 minute
Ue4/ue5 multi thread development attachment plug-in download address
神经网络c语言中的指针是怎么回事
Detailed explanation of Cocos creator 2.4.0 rendering process
Three. JS introductory learning notes 03: perspective projection camera
Asynchronous application of generator function
Android -- jetpack: the difference between livedata setValue and postvalue
VS2005 strange breakpoint is invalid or member variable value cannot be viewed
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
How to deploy the super signature distribution platform system?
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
Getting started with webgl (3)