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 :wink:

![alt text][1]
https://forum.unity.com/attachments/a-png.341227/

[1]: not found

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.