Flashlight rotation.


I’m trying to make a flashlight for a 3rd person project (no sockets involved, just a spotlight component attached to the default character’s collision capsule), and I want to be able to rotate it, for starters, up to 45 degrees right or left, but no more.

I have an Input Axis bound to Mouse X, as I want to be able to rotate the camera with my mouse. My blueprint setup is as follows:

When I hit Play, the flashlight goes haywire. If I’m looking forward, the flashlight constantly switches between two positions, and if I look backwards, it stays put, but I am able to rotate the flashlight the full 360 degrees instead of the rotation being capped at 45 degrees.

Any idea what could I be doing wrong? I’ve tried several methods and either the flashlight goes crazy or I can rotate it 360 degrees no problem.

Thanks in advance.

In something like this you shouldn’t be using the viewport size to control the rotation of an actor (in this case a light). The viewport size changes depending on the resolution the player has chosen which means the overall experience will change depending on the screen size.

You are also using a lerp where it’s alpha should only ever range from 0 - 1. 0 = A and 1 = B, 0.5 equaling something in between A & B. The current connection for the alpha will give strange results because your deriving values from the screen size that far exceed 0 - 1 range.

You are also then adding a large rotation to the current rotation, (The current rotation ± 45) This is going to cause the rotation to snap frantically and cause the crazy behavior your experiencing.

I’m going to assume that the “Axis Value” in the input config is set to 1. This then means that if the mouse moves to the right it equals 1 and if it’s moved to the left it equals -1 and of course if stationary it will equal 0. From this make a rot and plug the axis value into Y (yaw) and plug it straight into your add rotation node. This will cause the rotation of the light to increase/decrease by the axis value every tick the mouse is moving in the X direction.

Before this you would then want to do a check to limit the light from rotating past what you want but I’ll let you try have a go first. :wink: You might want to learn a little more about some of the nodes first though, hovering over a node will in most cases give you a description of what it does.

Hi, pattym, first of all thanks for your help.

I guess my post was a bit incomplete. What I am looking for is a way to limit the rotation. Plugging the axis value into the yaw of a new rotator and connecting said rotator to the “Add Local Rotation” node serves me to get a flashlight that can rotate 360 degrees in the horizontal axis. It was the first thing I did, just to have a base.

I didn’t know that the axis values could only be 1,-1 or 0. That error alone made me try to normalize the axis value (with the logic that you saw in my screenshot) to the [0,1] range, and was driving me crazy to limit the rotation.

I will keep trying, being a bit wiser now. Wish me luck :P.

Done. That information you provided was the key. It was easy once I knew that the axis value could only be 0, 1 or -1. With that information, I just implemented a counter and everytime the axis value was not 0 (and the counter was within my desired bounds, in this case plus/minus 45 degrees) I added or subtracted a few degrees to the desired rotation axis (pitch or yaw). Here is a screenshot of the vertical axis (only difference with the horizontal one is that it uses pitch instead of yaw), in case someone finds it useful.

Thanks again, you unstuck me :smiley: although I still need to check a few things regarding the rotation. When I have the two axes activated it does weird stuff, but with only one axis activated it works fine.

Edit: I replaced the “Add Local Rotation” nodes for “Add Relative Rotation” nodes and fixed those last few problems. Now all that’s left is to tweak.

Well, I set the definitive system up. I found out that axis value can be anything in the end, so with a clamp and a counter I think I nailed it this time.