- Home /
Aim down Sights Idea
@Bunny83 I thought about a script to get the relative fpscamera position into my hand coordinate system. The Idea is to move the hand right into the center of my fpscamera (my Aim Down Sights Idea):
script (lerp included):
Code (CSharp):
GameObject handAK47 = GameObject.Find("FPSController/FirstPersonCharacter/WeaponHolder/M4 Garand Holder/FPS-AK47/Game_engine/Bone/AK-47");
GameObject fpscamera = GameObject.Find("FPSController/FirstPersonCharacter");
transform.localPosition = Vector3.Lerp(handAK47.transform.localPosition, handAK47.transform.InverseTransformPoint(fpscamera.transform.position), Time.deltaTime * adsspeed);
... maybe someone know what I am doing wrong ;)
Answer by Bunny83 · Dec 13, 2018 at 11:40 PM
Well your code does in deed give you the position of the camera in local coordinate space of the "handAK47" object. However i think you misunderstand what a local space is and what the localPosition of an object actually specifies.
First of all keep in mind that your camera position is behind your near clipping plane. So if you move an object to that position it will most likely vanish since it's essentially behind the viewing frustum. If you want to move something into the center of the viewing frustum you need a position that is somewhere in front of the camera.
The next problem is the localPosition of an object is not relative to itself but relative to the immediate parent. In other words the localPosition of your "AK-47" object is relative to your "Bone" object. So that position specifies the position of the AK-47 object within the localspace of the "Bone" object. So if you want to calclulate a local target position for your AK-47 object you need to calculate it inside the localspace of the Bone object.
So you want to do something like this:
public float distance = 3f;
public float adsspeed = 1f;
Transform fpscamera;
Transform handAK47;
Transform.bone;
void Start()
{
fpscamera = GameObject.Find("FPSController/FirstPersonCharacter").transform;
bone = fpscamera.Find("WeaponHolder/M4 Garand Holder/FPS-AK47/Game_engine/Bone");
handAK47 = bone.Find("AK-47");
}
void Update()
{
Vector3 target = bone.InverseTransformPoint(fpscamera.position + fpscamera.forward * distance);
handAK47.localPosition = Mathf.Lerp(handAK47.localPosition, target, Time.deltaTime * adsspeed);
}
Note that i just copied your setup. Using Lerp this way is generally not recommended as this will be frame rate dependent. Also i changed the position of the sk47 object instead of the transform as the way you used Lerp would only make sense if you actually moved the object that you pass in as first argument.
It's unclear where this script is attached to. It would generally be recommended to avoid such hardcoded hierachy path strings and just work with public Transform variables that you can assign in the inspector.
Instead of messing with the local position yourself, why don't you just set the world space position of the object you want to move? The worldspace target position would be just
fpscamera.position + fpscamera.forward * distance
This is a worldspace point that is "distance" ahead of the camera. Just set the "position" of the object you want to move to this object. Unity automatically applies the required changes to the localPosition.
@Bunny83 Do you know coincidentally how the script looks like in ue4? Because I want to make own ADS animation with more than one bone in ue4.
Please stop posting comments all over the place to other questions. I'm not the only one answering questions and my work here generally is targeted towards the community and not single persons.
If you have a question that is significantly different from your question here, ask a seperate question. $$anonymous$$eep in $$anonymous$$d that this is UnityAnswers and we expect questions about the Unity engine. So questions towards the Unreal engine do not belong here at all. If you need information about the unreal engine, look for the unreal community.
If an answer doesn't answer your question properly you should think about how you can improve your question, add more details and be more specific what you want and what your actual problem is.
Your original question was just about getting the position of the camera inside the localspace of one of your child objects. $$anonymous$$ore specifically you tried to move that object in front of the camera. I explained quite detailed what issues you have in your approach and how it could be fixed.
You pointed out several times in your comments that you see$$anonymous$$gly have multiple bones per arm and that you want to move the hand in front of the camera. This simply isn't enough information to answer anything. Generally a humanoid character that has skeleton bones actually never "moves" any of the bones but only rotates them (pretty much like a normal human body). $$anonymous$$oving the weapon hand into a ADS position is usually done like any other animation: in a modelling / animation tool. Doing this via script with a multi bone hierarchy would involve inverse kinematics which is not only very complicated but also does not necessary produce a unique result. A normal human arm (just counting shoulder, elbow and wrist joint) represents a 7 degree of freedom kinematic chain (shoulder(2), upper arm twist(1), elbow(1), lower arm twist(1), wrist(2)).
Doing inverse kinematics manually is quite hard. You may want to look into Unity's I$$anonymous$$ support for humanoid models. However since the center of the screen of a FPS shooter is usually a well known / fix point within the character setup it would be way easier, cheaper and more consistant to just animate the move.
@Bunny83 Do you know how 'InverseTransformPoint' works or just how i can calculate from world to local space by hand (Sockets/Bone included)?
Your answer
![](https://koobas.hobune.stream/wayback/20220612183850im_/https://answers.unity.com/themes/thub/images/avi.jpg)