How to get player camera to point at a target?

Hey everyone! I hope somebody can help me out here. I have my player character that auto rotates his character towards the attack target every time its time to attack. This happens on tick if the ‘should rotate’ bool is true (meaning you are not pointed at the character so point at it). When on tick it runs the rotate if its true, i now also want it to pan the camera to point at that target as well IF you’re in 1st person mode. In this case, i AM in first person so i run the function to rotate the camera.

For whatever reason i cant seem to get it to rotate correctly, or if it does start rotating correctly towards the target, its stuck on some weird rotation when panning up/down. You can see that here in the video below. I also have included an image of the code im using. Additionally, if i start running around elsewhere, the rotation of where the camera should be is now messed up as well. Any input is greatly appreciated!

Hey @8DrunkenGods!

Have you tried breaking your character’s rot and only using the Z from that to rotate your camera? You definitely don’t want to take the X at all, because that’s where you’re getting that tilt (roll), that needs to stay 0.
image

Also, instead of the player’s rotation, you can use the camera’s rotation. Then I’d use world instead of relative for all of this.
But that’s just freehand, I didn’t test it. If you do this and come back I may have more insight if it’s going wrong from there. :slight_smile:

1 Like

Thanks a lot for the reply! I’ve made the changes you suggested and its definitely along the right path now since its perfectly changing camera’s view directly at the target which it was not doing right away before. Here are the changes (and cleaned it up a bit) that i made in the image below. You can see i did not use the camera rotation as it doesnt seem to actually work when i use the camera’s rotation for some reason. Regardless if i plug in the Pitch of the Target Rotation break and make, it doesnt seem to make any difference whatsoever. It also still rolls randomly in directions when i move mouse up or down. Basically the changes just made it perfectly align the camera to the target, but thats still a step forward!

Here is a video showing the 1st change using the character rotator, then a 2nd go where i use the camera rotator (that doesnt work).

sorry i uploaded the wrong image, updated the post above.

Hmm. So I think the issue may lay with your player character’s BP.
What is the camera attached to? Are you still using a springarm in the first person camera? If so you may want to uncheck “Inherit Roll” (that’s the X value). What I’m thinking is:

It’s attached to the skeletal mesh, so it does the animations along with the mesh, which includes X as a value of movement.
When you then move the mouse, it KEEPS the X variable as a local rotation movement, and the new X is now the camera’s relative 0, which would be fine if the mouse could control that, but the mouse controls pitch and yaw (Y and Z). And it gets worse and worse with no way to correct. Try it with no animations on your character active, if it doesn’t mess up, that’s definitely the issue. :slight_smile:

Ya, the 1st person camera is attached to the mesh. There is the spring arm as the parent as well. I disabled the inherit roll but now the camera is sideways in 1st person mode haha. This lead me to seeing that the camera was indeed tilted the wrong way so i rotated it back to a 0 on that axis as it should be. Now weather i have inherit true or false, the camera is positioned correctly, however it still curves left to right or up/down when moving the mouse up or down in 1st person mode and randomly changes as the view target changes.

So i paused aimations and it made no difference :frowning:

edit:
So you can see here in the video below that i ran 2 tests, each with 2 differences (so 4 test total)
Test 1a: Using camera to set rotation while using inherit roll false.
Test 1b: Using camera to set rotation while using inherit true.
Test 2a: Using Camera Spring arm to set rotation while using inherit roll false.
Test 2b: Using Camera Spring arm to set rotation while using inherit roll true.

Some extra details:
*I created a new spring arm and camera for this test, just to be 100% sure the camera i had copy/pasted didnt somehow have some issues. This ultimately made no difference.
*The spring arm is attached to the skeletal mesh ‘Head’ socket.

Maybe you’ll see something that red flags to you, or you can clarify which one i should be setting rotation for, the spring arm or the camera.

Did you try attaching it to the collision capsule instead? Or just have its parent be the actor itself instead of another component? It’s possible that it’s the socket attachment that is the issue.

When i put the camera (no spring arm) onto the capsule, while it still works when i toggle into that view, the ‘set rotation’ no longer works and it just never changes camera rotation.

If i put it back into the spring arm while its still under the capsule comp, it acts the same as all previous tests.

I’m out of idea’s to try, it literally makes ZERO sense. Its so ■■■■ simple…just a 1st person camera that player can freely look around their character from the head point of view, plus it forcibly updates your viewpoint to point at your target when in battle (the function im calling). That simple lol.

I suggest you do not change the rotation of the component directly if the camera is under any spring arm component, instead use “set control rotation” from the player controller, the spring arm component reads its target rotation from the controller.

image

Doing it this way, as long as your camera is under the spring arm component, it should correctly change its rotation.

1 Like

Sorry! I was meaning attach the spring arm to the capsule.

As @octolex was saying, if it’s attached to the springarm, that’s what you want to control. You basically let the camera remain 0,0,0 at all times, and let the springarm be the moved or component - if you have one. Not all builds will use one.
Try what octolex suggested and get back to us!

1 Like

That did it! That 1 node, its always something so simple that i spend hours on :sob:
Thanks a lot! This was the solution.

Thanks a lot to you as well @Mind-Brain , you helped me throughout the whole thing and i cleaned up my code a bit in the process haha. Thanks!

2 Likes

I’m glad it works! ^^

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