|
I have a Gun in my scene and I would like to limit its rotation, but not around two axes like the mouselook script does. Instead I'd like to limit it to a solid angle around a single axis. I'm working on a solution right now, but it seems to be way too complicated. Does anyone know a simple reliable method for this?
would be the maximal allowed rotatin. Edit: The object should be able to rotate around its pivot point, or the zero point of its local coordinate system, but its rotation in all the directions of its local xy plane should be limited to .
(comments are locked)
|
|
Made something that should act like the old code, but run faster, and use less code. You might also want to do a raycast where you set the target variable and find the actualdistance, instead of using 20. Feel free to change the way limit is calculated back to your way. I just like that when limitRadius is larger, the circle is larger. You basically limit movement around the (0, 0) scrren coordinates and than move the ponit to the coordinate system with the screen center as its zero point by additions and substractions, right?
Nov 04 '10 at 10:51 AM
LeiterJakab
origo = Vector2(Screen.width / 2, Screen.height / 2); limit = Screen.width * limitRadius; These lines should be in Update so that changing the resolution wouldn't effect the radius and center of the limt area.
Nov 04 '10 at 10:56 AM
LeiterJakab
I just tried to make the code run as fast as possible. I didn't think that that the user might fullscreen the game some time, so feel free to move them back to update(and I moved all the code to update because it gets called less frequently than OnGUI). And yes, that is basically what I do
Nov 04 '10 at 12:32 PM
Atnas1010
Thanks a lot very nice script.
Nov 04 '10 at 07:20 PM
LeiterJakab
(comments are locked)
|
|
Well this is how I solved this problem. Since I needed a crosshair for the gun in my scene anyway, I just control the crosshair GUI instead of the gun. I limited its movement to a circle around the center of the screen, converted screen coordinates to world coordinates and used the transform.LookAt function to turn the gun towards that point. There must be a better way to do it, or maybe there isn't. I'm still interested in ideas. I'll have a look at it. Is this exactly how you want it to behave in the end? And radius gets inverted somewhere..?
Nov 03 '10 at 09:57 PM
Atnas1010
Or would you like the user to be able to move his mouse outside the circle, and then have to move it all the way back before the crosshair would leave the edge of the circle?
Nov 03 '10 at 10:19 PM
Atnas1010
At first I used the limitRadius var, to check if currentRadius was bigger than this and I used it in the ClampMagnitude function to limit the movement to a circle area. Basically similar to what IJM did so thanks for that idea. However I noticed that this would result in different movement angles with different resolutions. Thats why I used the line: limit = Screen.width / limitRadius; This way entering 2 will always result in the limit radius being half the screen width.
Nov 03 '10 at 10:37 PM
LeiterJakab
(comments are locked)
|
|
I think that this will work:
p.s. I din't test this code. p.p.s. Yes it works ;) I just made a small test. I can't try it right now, but doesn't this limit the rotation to +/-60 degrees around the 'x' axis?
Nov 01 '10 at 04:14 PM
LeiterJakab
Yes, for the GameObjects local x axis. (Just try it)
Nov 01 '10 at 06:44 PM
IJM
Sorry if my question wasn't clear enough, I've edited it.
Nov 01 '10 at 10:52 PM
LeiterJakab
(comments are locked)
|
|
I just wrote this little ditty the other night. According to the diagram in your post, and if my understanding (that you want to limit the angle of deviation from a single axis/vector) is correct, this script should prove useful for you. The rotation is restricted on
(comments are locked)
|
|
This worked well for me:
Note: In my case, -myTransform.right is the vector that faces the camera
(comments are locked)
|

