i have this script that makes a gun rotate based on an other gameobjects rotation(i dont want to use mouse input)
it works fine mostly but sometimes “overshoots”
so something is wrong with my rotation calculation but i cant figure it out.
any ideas what causes this? how do fix it?
tnx
A few things are confusing. Why are there two transforms:
public Transform rotatorx;
public Transform rotatory;
Is this two different objects? Or is it the same object your just dragging its transform in twice? If its two different objects thats going to produce some weird results with getting the x angle of Object1 and the Y angle of Object2. If its just 1 object then you can easily do this and it would make the code a bit cleaner and more readable:
Its also a little unclear how you want rotator object and your gun to interact. Is the idea that your gun is at some Specific rotation, call it R. If the rotator object rotated 10 degrees around the Y-axis, then you want your gun to rotate that same 10 degrees. so it would be R.y+=10? it seems like thats what your trying to do, but this line here:
Would always be trying to move the gun’s absolute rotation to the rotations difference in the rotator. Not change its rotation relative to the relative difference of the rotator. (Since WantedRotation is not an absolute rotation, just the difference between the last 2 checks on rotator’s rotation, while localRotation is indeed an absolute Rotation.)
Unless of course the gun is the child of the rotator?
the two rotators are the player for y and the main camera for x.
its a 3d game.
the gun is a child of the main camera and player.
i think it overshoots because the rotation difference changes from say positive to negative at a 360 degree switch, but im not sure
This is a little confusing, not sure how it can be child of both. How is your hierarchy set up? I’m guessing from that line that’s its a 3rd person game. The camera is a child of the player and the gun is a child of the player? Or is it something totally different. With using other objects rotations it will be important to know to how things are setup to figure out the math of your code.
im basicly trying to make my “gunspring” object add some rotation to the gun models based on " maincamera, for x"
and " the player, for y". but i dont want to use mouse input for this so i want to use the 2 root objects as the sources for the rotation.
I assume player in “the player, for y” is fpsplayerBase? also not sure which camera is “maincamera” since you dont have a “maincamera” listed in that hiearchy you posted. Remember we don’t have access to your project, or how you set it up. You might know that when you say player you mean fpsPlayerBase, or maybe you mean playerController but we don’t. Especially, we I have no clue what is meant by “maincamera”.
Anyway, if your gunspring is a child of the player. When your player rotates around the Y-Axis (spins around looking)… your gunspring (and thus all the guns that are children of it) will rotate by that same amount as well. Why do you want to rotate them further around the Y-Axis?
playercontroller is player Fpcamera is maincamera
when the player or camera rotates i want the guns to be rotated a bit extra to simulate some “spring” , so when nothing moves the guns go back to center position
So the idea is that I turn quickly to the right, but my gun lags a little behind me but in a fraction of a second or so it snaps to the forward position.
And you say it “overshoots” you mean instead of lagging behind you in the spin, it goes too far then snaps back?
no if i move to right the gun moves a little bit more to the right but the idea is the same.
problem is when hitting a 180 rotation angle the gun makes a slight twitch thats what i meant with overshoot