- Home /
Gun stretches when rotated
Does anyone know why this is happening?
normal:
rotated:
Answer by Eno-Khaon · Jul 18, 2020 at 06:57 PM
This stretching occurs as the result of a parent Transform's non-uniform scale being applied to a child Transform. This becomes especially visible during rotation, when that 3-axis scale attempts to apply itself to a child Transform at any, but especially at any non-90-degree rotations.
A simple solution to this is to ensure that a GameObject's parent Transform has a uniform scale Vector3.one * scale
(for example, by putting both parent and child objects into a separate, unscaled container instead).
Since I couldn't get images to upload in a comment, I edited this answer with the intended comment to include the intended images.
Certainly! Here's a step-by-step (from scratch) on what you're seeing, vs. how you can work around it:
1) Create two cubes, then drag one onto the other in the Hierarchy to make it the child object.
2) Scale up the parent object on a single axis. The child will scale along with it. Note at this time that the child's scale is still (1, 1, 1).
3) Rotate the child. It will deform heavily during the rotation, since it's being stretched along a fixed axis by its parent.
4) To deal with this: Create an Empty GameObject. This will act as the new "parent" to the child instead. When you drag it onto the Parent Cube to make it a child, Unity will automatically calculate the inverse scale to ensure that its scale becomes (1, 1, 1), relative to its parents. Additionally, ensure that it has no rotation relative to the Parent Cube (otherwise, you will see distortion again).
5) Rotate the Child Cube again. This time, it will behave as you intend for it to.
Thank you @$$anonymous$$o-Khaon for your response. I am kind of new to unity, so is there a way to explain it step by step so I can follow along?
Of course! I edited my answer to include a more detailed breakdown of how to replicate both the intended and unintended outcomes.
Update: I fixed the stretching, but I don't know if I set it up like you explained. Here is a screenshot of what is happening:
Answer by tuinal · Jul 18, 2020 at 12:29 AM
All objects have a pivot (origin) around which they scale and rotate. If this is not in the point you want it to rotate around, you'll get this kind of behaviour.
This is set in the modelling software rather than unity. If you're using Blender, select a vertex on the grip in edit mode > shift S cursor to selection > object mode > object > set origin > origin to 3D cursor.
@tuinal, thank you for your time, but that didn't seem to help the problem. I followed your steps word by word, and it just didn't seem to work.
It it's editor rotation, you have 'centre' on at the top of the screen (next to global), this needs changing to 'pivot' by clicking on it.
nb the pivot point in blender is shown as a yellow dot in object mode - you want it in the middle of the grip - it's easy to accidentally click elsewhere in the steps above which can move the 3d cursor accidentally.
& if none of that works try selecting the object and object > apply > all transforms before the steps above. )
Just tried what you said, that doesn't seem to work either :(
Answer by RynthGames · Jul 18, 2020 at 11:14 PM
Okay, so first we need to know how you've created your gun. Have you modelled the gun in a separate application such as Blender or 3DS Max, or have you just made it from cylinders using Unity by itself? Judging by the names of the objects, it looks like you've just cobbled together some cylinders in Unity, which is fine but this makes things a little more complex...
So, if you're going to make your gun in Unity, I'd suggest starting with an empty GameObject which you can name "Gun", and then put a couple of cylinders inside of there to represent the grip and barrel. Now the important thing is that you leave the position, rotation, and scale of your "Gun" object at (0,0,0), (0,0,0) and (1,1,1) - this is going to save you a bunch of headaches later on.
With this in mind, adjust the position, rotation, and scale of your cylinders to model your gun - you shouldn't need to mess with the transform of "Gun" to do this. One thing to remember is that you need to position your grip and barrel so that they lie at a suitable point in relation to the origin (0,0,0), as the origin is the point around which your gun will rotate.
Hopefully this will help a little. Just give us a shout if you need any more help (I don't know how new to games development you are, but remember we all have to start somewhere)!
Your answer
Follow this Question
Related Questions
Convert Object Rotation to X, Y values 1 Answer
Getting cosinus of an angle in degrees 3 Answers
How can I rotate WheelCollider around local axis? 0 Answers
Limit on x rotation axis in play mode 2 Answers