Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michael_R · Dec 12, 2018 at 09:28 PM · scripting problemfps

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 ;)

alt text https://forum.unity.com/attachments/a-png.341227/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michael_R · Dec 15, 2018 at 05:51 PM 0
Share

@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.

avatar image Bunny83 Michael_R · Dec 22, 2018 at 02:21 AM 0
Share

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.

avatar image Michael_R Bunny83 · Jan 02, 2019 at 03:54 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get bullets to hit crosshair 2 Answers

How To Convert World/Relative Position for Aim Down Sights? 1 Answer

My player's camera displays jerky objects 2 Answers

Animations interupting Aim-Script 0 Answers

MY FPSE`S fps is not moving need help! I tried all solutions! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges