Help with getting gun to point at player + math

This only points the base in the players direction though, right? I need the turret gun hinge constraints to also change depending on the player’s position

This will solve the problem you have with the base rotating incorrectly. As for how to calculate the angle for the turret gun’s HingeConstraint, I wish I could help you but my math knowledge isn’t advanced enough to do so unfortunately

1 Like

I did some research and I found some useful information: If the base is rotating correctly, then the gun only needs to rotate up and down to point at the player. Due to this, we can use 2D math instead of 3D math since we’re dealing with only 1 axis so we only need triangle calculations

This website has an angle of a triangle calculator, and also shows the formulas you need to calculate them

1 Like

The HingeConstraints.TargetAngle is measured in degrees I believe.

The calculation shows the angle is 16, and the further away I get the angle increases. Shouldn’t it decrease the further away you get? It also appears the current angle isn’t equal the target angle, which may be part of the problem

Yea that seems useful. However, we are only dealing with two known sides, distance between the player and the turret (hypotenuse), and the distance from the ground. I believe the formula for that is -1cos(adjacent side / hypotenuse)?

Perhaps I need to find a better way of making a turret like this? I’ve observed that for some reason the parts that are connected by the HingeConstraints are delayed when the base is moved, so as a work around I was just welding those parts to the base, and then unwelding them. I don’t think this would work though when rotating the gun and base at the same time.

Maybe I need to change the way I’ve set up the turret, and use an alternative way without hinge constraints? I’ve found this video on youtube: https://www.youtube.com/watch?v=VUegcu8HSfM

I think maybe I should just get the nozzle to always point at the player, and somehow disconnect it from the rest of the base so the base doesn’t rotate, only the nozzle

Is the distance from the ground the orange arrow or the white arrow

White (silly char lmit blahblahblah)

If I’m not mistaken (I haven’t read all the posts yet) you can just use the CFrame.new(Position, LookPosition) to make an object/cframe point towards something. Then just use CFrame.angles and mutliply that with the CFrame you get for a rotational offset.

PS: If you’re wondering why I just don’t get the nozzle to follow the root parts position

I don’t think that should be an issue if you set the nozzle cframe manually.

1 Like

Yes, but the turret has two main parts. The base and the nozzle, the base does exactly what you said, but the nozzle needs to point towards the player using hinge constraints. Kinda hard to explain, but watch this video

image

What if instead of hinge constraints, you use a model and use the PivotTo() method of the model to set the primary CFrame of it?

1 Like

Could you elaborate more? Like have the nozzle in a separate model to the base?

Then the calculation needs to be either:
math.asin(white arrow / hypotenuse)

or:

local magnitude = math.sqrt(hypotenuse^2 - white arrow^2)

math.asin(magnitude / hypotenuse)

You need to convert the answer to degrees using math.deg though

1 Like

I’ve just accidentally done this by forgetting to comment out a line, and it appears to work? It doesn’t use the Hinge, but that’s fine with me. I think this is a solution

base.CFrame = CFrame.new(base.Position, Vector3.new(tHumPart.Position.X, base.Position.Y, tHumPart.Position.Z))
nozzle.CFrame = CFrame.new(nozzle.Position, tHumPart.Position)

Feel guilty for marking my own post as the answer, but thank’s a lot to @JohhnyLegoKing, @BuilderDolphin and @creepersaur for giving up your time

2 Likes

Yeah exactly. The base rotating is fine, but you can separate the nozzle into a model, making it the primary part of the model and using PivotTo() on that. It’ll make the maths way easier (hopefully), and you wont have to use hinges.

1 Like

Looks great to me :grin: 30charlimit

1 Like

Yea it works with just those two lines. Was expecting it to be way harder

1 Like

Appreciate your help, can’t believe we didn’t need all that trig after all. Also saves the use of HingeConstraints, since they are a bit glitchy!

1 Like

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