Tank knockback effect doesn't work as it should

Hello. I have no idea how I’m going to describe this issue in detail, so I allowed myself to record it instead to show you what the issue is. I wanted to create a knockback system so that when the tank’s turret fires, the tank suffers knockback from it. Simple enough right? The problem is that it only works when the vehicle’s chassis is facing one direction. If it faces let’s say the opposite direction, then the knockback is exactly the opposite. I mean exactly the opposite, take a look:


^ - the beginning works just fine, and then it goes haywire when the orientation of the chassis changes.

Here is the code responsible for this mess which I’ve changed probably around a hundred times to fix but failed:

				local turretDirection = script.Parent.MainTurret.PrimaryPart.CFrame.LookVector

				-- Calculate the opposite direction
				local oppositeDirection = -turretDirection

				-- Define the rotation angle (45 degrees downwards)
				local rotationAngle = math.rad(-15)

				-- Calculate the rotation in the opposite direction and downwards
				local rotation = CFrame.fromAxisAngle(oppositeDirection:Cross(Vector3.new(0, 1, 0)), rotationAngle)

				-- Apply the rotation to the part
				part.CFrame = part.CFrame * rotation
				task.wait(2)

(part - vehicle’s chassis)

The worst part is that after waking up today I understand even less what this code does than when I was writing it yesterday. Although I do remember skipping the middle-man which is the turret and going straight for the target only to get the same results as in the video. If need be I can try to find that code which goes straight for the target part if nobody has any idea what to do with that chunk of code above. I just gave up on fixing it myself and now I’m here as my last resort.

2 Likes

Hello, can you try this script and let me know if it works for you?

local turretDirection = script.Parent.MainTurret.PrimaryPart.CFrame.LookVector

-- Calculate the opposite direction
local oppositeDirection = -turretDirection

-- This moves the vehicle opposite to the turret's facing direction
local knockbackStrength = 10 -- you might need to change it
local knockbackCFrame = CFrame.new(oppositeDirection * knockbackStrength)

script.Parent.Chassis.CFrame = script.Parent.Chassis.CFrame * knockbackCFrame

:smile:

1 Like

Same issue persists unfortunately.

I tried playing with CFrame.Angles but to no avail, even tried multiplying the chassis CFrame by CFrame.Angles(math.pi/2,0,0) but either I did it the wrong way or it doesn’t work. Help is greatly appreciated.

This method seems to be working well when testing:

local turretDirection = script.Parent.MainTurret.PrimaryPart.CFrame.LookVector

local rotationAngle = math.rad(45)

local rotation = CFrame.fromAxisAngle(turretDirection:Cross(Vector3.yAxis), rotationAngle)

part.CFrame = CFrame.new(part.Position) * rotation
task.wait(2)

The main difference to your current formula is that I’m using the turret’s LookVector directly instead of its negative LookVector, the rotationAngle is 45 degrees in radians instead of -15 in rad, and I’m multiplying the rotation CFrame with a CFrame value that consists of only the part’s position

1 Like

It unfortunately also appears to be not working. I used your code and it worked in the beginning as demonstrated in the video, but as the chassis rotates, it ceases to work properly. The problem only appears the moment the chassis, or the ‘part’ changes its orientation.

Using part.CFrame:ToWorldSpace might fix the problem:

local turretDirection = script.Parent.MainTurret.PrimaryPart.CFrame.LookVector

local rotationAngle = math.rad(45)

local rotation = CFrame.fromAxisAngle(turretDirection:Cross(Vector3.yAxis), rotationAngle)

part.CFrame = part.CFrame:ToWorldSpace(rotation)
task.wait(2)
1 Like

It’s a good try and it did work with other components like the turret and making it accurately face the target with hingeconstraints (with ToWorldSpace command), however it didn’t work here. Indeed, the issue still persists and the result is pretty much the same as in the original video.

1 Like

I’ve tested a lot of different methods, but unfortunately all of them seem to have some kind of drawback so I don’t feel comfortable suggesting them

I do recommend that you test some ways to achieve the desired recoil using physics constraints or methods such as BasePart:ApplyImpulse, they might be able to solve the problem without requiring a lot of CFrame & vector math, although at a cost that parts will need to be unanchored and also dealing with possible network ownership issues

I wish you luck in finding a solution, and if I find a good result I’ll be sure to let you know :slight_smile::+1:

1 Like

try this

				part.CFrame = (rotation*part.CFrame).Rotation + part.CFrame.Position

Uses the rotation in world space formula by applying the rotation in the left hand side of the equation.

3 Likes

This is how I felt after using your code:
ezgif-4-80c4ba82cb

Side note: It’s unbelievable how short and simple the fix was. You have my great thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.