Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JuanseCoello · Dec 22, 2014 at 10:51 AM · convert

Could you help me convert this script to C#?

I am working in an inverse kinematic system. A very simple one to make foot Ik. I need this script in C#. Sorry to ask this, but I have no idea on how JS works, I prefer to work in C#. Thank you.

        var upperArm : Transform;
        var forearm : Transform;
        var hand : Transform;
        var target : Transform;
        var elbowTarget : Transform;
        var IsEnabled : boolean = true;
        var debug : boolean = true;
        var transition : float = 1.0;

        private var upperArmStartRotation : Quaternion;
        private var forearmStartRotation : Quaternion;
        private var handStartRotation : Quaternion;
        private var targetRelativeStartPosition : Vector3;
        private var elbowTargetRelativeStartPosition : Vector3;

        function Start(){
 upperArmStartRotation = upperArm.rotation;
 forearmStartRotation = forearm.rotation;
 handStartRotation = hand.rotation;
 targetRelativeStartPosition = target.position - upperArm.position;
 elbowTargetRelativeStartPosition = elbowTarget.position - upperArm.position;

}

      function LateUpdate () {
 if (!IsEnabled){
     return;
 }
 CalculateIK();

}

       function CalculateIK(){
 //Calculate ikAngle variable.
 var upperArmLength : float = Vector3.Distance(upperArm.position, forearm.position);
 var forearmLength : float = Vector3.Distance(forearm.position, hand.position);
 var armLength : float = upperArmLength + forearmLength;
 var hypotenuse : float = upperArmLength;
 var targetDistance : float = Vector3.Distance(upperArm.position, target.position);    
 targetDistance = Mathf.Min(targetDistance, armLength - 0.0001); //Do not allow target distance be further away than the arm's length.
 //var adjacent : float = (targetDistance * hypotenuse) / armLength;
 var adjacent : float = (Mathf.Pow(hypotenuse,2) - Mathf.Pow(forearmLength,2) + Mathf.Pow(targetDistance,2))/(2*targetDistance);
 Debug.Log(adjacent);
 var ikAngle : float = Mathf.Acos(adjacent/hypotenuse) * Mathf.Rad2Deg;
 //Store pre-ik info.
 var targetPosition : Vector3 = target.position;
 var elbowTargetPosition : Vector3 = elbowTarget.position;
 var upperArmParent : Transform = upperArm.parent;
 var forearmParent : Transform = forearm.parent;
 var handParent : Transform = hand.parent; 
 var upperArmScale : Vector3 = upperArm.localScale;
 var forearmScale : Vector3 = forearm.localScale;
 var handScale : Vector3 = hand.localScale;
 var upperArmLocalPosition : Vector3 = upperArm.localPosition;
 var forearmLocalPosition : Vector3 = forearm.localPosition;
 var handLocalPosition : Vector3 = hand.localPosition;
 var upperArmRotation : Quaternion = upperArm.rotation;
 var forearmRotation : Quaternion = forearm.rotation;
 var handRotation : Quaternion = hand.rotation;
 //Reset arm.
 target.position = targetRelativeStartPosition + upperArm.position;
 elbowTarget.position = elbowTargetRelativeStartPosition + upperArm.position;
 upperArm.rotation = upperArmStartRotation;
 forearm.rotation = forearmStartRotation;
 hand.rotation = handStartRotation;
 //Work with temporaty game objects and align & parent them to the arm.
 transform.position = upperArm.position;
 transform.LookAt(targetPosition, elbowTargetPosition - transform.position);
 var upperArmAxisCorrection : GameObject = new GameObject("upperArmAxisCorrection");
 var forearmAxisCorrection : GameObject = new GameObject("forearmAxisCorrection");
 var handAxisCorrection : GameObject = new GameObject("handAxisCorrection");
 upperArmAxisCorrection.transform.position = upperArm.position;
 upperArmAxisCorrection.transform.LookAt(forearm.position, transform.root.up);
 upperArmAxisCorrection.transform.parent = transform;
 upperArm.parent = upperArmAxisCorrection.transform;
 forearmAxisCorrection.transform.position = forearm.position;
 forearmAxisCorrection.transform.LookAt(hand.position, transform.root.up);
 forearmAxisCorrection.transform.parent = upperArmAxisCorrection.transform;
 forearm.parent = forearmAxisCorrection.transform;
 handAxisCorrection.transform.position = hand.position;
 handAxisCorrection.transform.parent = forearmAxisCorrection.transform;
 hand.parent = handAxisCorrection.transform;
 //Reset targets.
 target.position = targetPosition;
 elbowTarget.position = elbowTargetPosition;    
 //Apply rotation for temporary game objects.
 upperArmAxisCorrection.transform.LookAt(target,elbowTarget.position - upperArmAxisCorrection.transform.position);
 upperArmAxisCorrection.transform.localRotation.eulerAngles.x -= ikAngle;
 forearmAxisCorrection.transform.LookAt(target,elbowTarget.position - upperArmAxisCorrection.transform.position);
 handAxisCorrection.transform.rotation = target.rotation;
 //Restore limbs.
 upperArm.parent = upperArmParent;
 forearm.parent = forearmParent;
 hand.parent = handParent;
 upperArm.localScale = upperArmScale;
 forearm.localScale = forearmScale;
 hand.localScale = handScale;
 upperArm.localPosition = upperArmLocalPosition;
 forearm.localPosition = forearmLocalPosition;
 hand.localPosition = handLocalPosition;
 //Clean up temporary game objets.
 Destroy(upperArmAxisCorrection);
 Destroy(forearmAxisCorrection);
 Destroy(handAxisCorrection);
 //Transition.
 transition = Mathf.Clamp01(transition);
 upperArm.rotation = Quaternion.Slerp(upperArmRotation, upperArm.rotation, transition);
 forearm.rotation = Quaternion.Slerp(forearmRotation, forearm.rotation, transition);
 hand.rotation = Quaternion.Slerp(handRotation, hand.rotation, transition);
 //Debug.
 if (debug){
     Debug.DrawLine(forearm.position, elbowTarget.position, Color.yellow);
     Debug.DrawLine(upperArm.position, target.position, Color.red);
 }

}

Comment
Add comment · Show 8
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 HarshadK · Dec 22, 2014 at 10:59 AM 3
Share

Just one suggestion: try converting this script by yourself. It is not as hard as you might think. If there are few issues remaining ask people for help.

You can even use online converters like this one here.

Or rather post this conversion task on Forum to help make UA a better technical Q&A site.

avatar image taxvi · Dec 22, 2014 at 11:00 AM 0
Share

if you understand c# you must be able to get through this code easily. most stuff is the same as c# you only need to change 1. the variable declarations 2. function declarations 3. encapsulate this code in the class declaration 4. add the #import statements on top

the rest of the code like calling functions, for loops, etc. is the same

one thing to consider though is to make sure the privacy of the variables / functions is set as you need them. for example in JS declaring a variable without 'private' or 'public' keyword makes the variable public by default, while in c# the default is private.

good luck ;)

avatar image JuanseCoello taxvi · Dec 22, 2014 at 11:29 AM 0
Share

Thanks, I already converted my file into C#, but I am not sure if it is correct. Could you check it if everything I did is fine? Because I speak spanish, and is difficult for me to completely do$$anonymous$$ate the scripting language, because there are many things I dont quite understand.

I uploaded an attachmentlink text

iklimb.txt (5.4 kB)
avatar image HarshadK · Dec 22, 2014 at 11:35 AM 0
Share

Read and looks good to me. Now you just need to check if it compiles and runs properly and then you have done it.

See, it's not that hard. ;-)

avatar image JuanseCoello · Dec 22, 2014 at 11:47 AM 0
Share

I already converted this file by myself, but it is not working. Specially the public transforms doesnt appear.

And this line gets an error

upperArmAxisCorrection.transform.localRotation.eulerAngles.x -= ikAngle;

I cant do more than that. Please help!

avatar image JuanseCoello · Dec 22, 2014 at 11:52 AM 0
Share

I cant do more than what I have done, and it is not working.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Dec 22, 2014 at 12:15 PM

There are two things that needed fix.

1) You need to append an 'f' after a float value. Like: public float transition = 1.0f;

2) And you need to store your result in a temporary variable and assign it to your rotation. I solved this in your script using:

 float newUpperArmAxisCorrectionX = upperArmAxisCorrection.transform.localRotation.eulerAngles.x - ikAngle;
 upperArmAxisCorrection.transform.localRotation = Quaternion.Euler(newUpperArmAxisCorrectionX, upperArmAxisCorrection.transform.localRotation.eulerAngles.y, upperArmAxisCorrection.transform.localRotation.eulerAngles.z);

Here's is a script without compiler errors for you:

 using UnityEngine;
 using System.Collections;
 
 public class ikLimb : MonoBehaviour {
 
 public Transform upperArm;
 public Transform forearm;
 public Transform hand;
 public Transform target;
 public Transform elbowTarget;
 public bool IsEnabled = true;
 public bool debug = true;
 public float  transition = 1.0f;
 
 private Quaternion upperArmStartRotation;
 private Quaternion forearmStartRotation;
 private Quaternion handStartRotation;
 private Vector3 targetRelativeStartPosition;
 private Vector3 elbowTargetRelativeStartPosition;
 
 
     // Use this for initialization
     void Start () {
 
     upperArmStartRotation = upperArm.rotation;
     forearmStartRotation = forearm.rotation;
     handStartRotation = hand.rotation;
     targetRelativeStartPosition = target.position - upperArm.position;
     elbowTargetRelativeStartPosition = elbowTarget.position - upperArm.position;
     }
 
     void LateUpdate()
     {
         if (!IsEnabled)
         {
             return;
         }
         CalculateIK();
     }
 
     void CalculateIK(){
     //Calculate ikAngle variable.
     float upperArmLength = Vector3.Distance(upperArm.position, forearm.position);
     float forearmLength  = Vector3.Distance(forearm.position, hand.position);
     float armLength  = upperArmLength + forearmLength;
     float hypotenuse  = upperArmLength;
     float targetDistance  = Vector3.Distance(upperArm.position, target.position);    
     targetDistance = Mathf.Min(targetDistance, armLength - 0.0001f); //Do not allow target distance be further away than the arm's length.
     //var adjacent : float = (targetDistance * hypotenuse) / armLength;
     float adjacent  = (Mathf.Pow(hypotenuse,2) - Mathf.Pow(forearmLength,2) + Mathf.Pow(targetDistance,2))/(2*targetDistance);
     Debug.Log(adjacent);
     float ikAngle  = Mathf.Acos(adjacent/hypotenuse) * Mathf.Rad2Deg;
     //Store pre-ik info.
     Vector3 targetPosition = target.position;
     Vector3 elbowTargetPosition = elbowTarget.position;
     Transform upperArmParent = upperArm.parent;
     Transform forearmParent  = forearm.parent;
     Transform handParent  = hand.parent; 
     Vector3  upperArmScale = upperArm.localScale;
     Vector3  forearmScale  = forearm.localScale;
     Vector3  handScale  = hand.localScale;
     Vector3  upperArmLocalPosition  = upperArm.localPosition;
     Vector3  forearmLocalPosition  = forearm.localPosition;
     Vector3  handLocalPosition  = hand.localPosition;
     Quaternion upperArmRotation  = upperArm.rotation;
     Quaternion forearmRotation  = forearm.rotation;
     Quaternion handRotation  = hand.rotation;
     //Reset arm.
     target.position = targetRelativeStartPosition + upperArm.position;
     elbowTarget.position = elbowTargetRelativeStartPosition + upperArm.position;
     upperArm.rotation = upperArmStartRotation;
     forearm.rotation = forearmStartRotation;
     hand.rotation = handStartRotation;
     //Work with temporaty game objects and align & parent them to the arm.
     transform.position = upperArm.position;
     transform.LookAt(targetPosition, elbowTargetPosition - transform.position);
 
     GameObject upperArmAxisCorrection = new GameObject("upperArmAxisCorrection");
     GameObject forearmAxisCorrection  = new GameObject("forearmAxisCorrection");
     GameObject handAxisCorrection  = new GameObject("handAxisCorrection");
 
     upperArmAxisCorrection.transform.position = upperArm.position;
     upperArmAxisCorrection.transform.LookAt(forearm.position, transform.root.up);
     upperArmAxisCorrection.transform.parent = transform;
     upperArm.parent = upperArmAxisCorrection.transform;
     forearmAxisCorrection.transform.position = forearm.position;
     forearmAxisCorrection.transform.LookAt(hand.position, transform.root.up);
     forearmAxisCorrection.transform.parent = upperArmAxisCorrection.transform;
     forearm.parent = forearmAxisCorrection.transform;
     handAxisCorrection.transform.position = hand.position;
     handAxisCorrection.transform.parent = forearmAxisCorrection.transform;
     hand.parent = handAxisCorrection.transform;
     //Reset targets.
     target.position = targetPosition;
     elbowTarget.position = elbowTargetPosition;    
     //Apply rotation for temporary game objects.
     upperArmAxisCorrection.transform.LookAt(target,elbowTarget.position - upperArmAxisCorrection.transform.position);
     
     float newUpperArmAxisCorrectionX = upperArmAxisCorrection.transform.localRotation.eulerAngles.x - ikAngle;
 upperArmAxisCorrection.transform.localRotation = Quaternion.Euler(newUpperArmAxisCorrectionX, upperArmAxisCorrection.transform.localRotation.eulerAngles.y, upperArmAxisCorrection.transform.localRotation.eulerAngles.z);
     
     forearmAxisCorrection.transform.LookAt(target, elbowTarget.position - upperArmAxisCorrection.transform.position);    
     handAxisCorrection.transform.rotation = target.rotation;
     //Restore limbs.
     upperArm.parent = upperArmParent;
     forearm.parent = forearmParent;
     hand.parent = handParent;
     upperArm.localScale = upperArmScale;
     forearm.localScale = forearmScale;
     hand.localScale = handScale;
     upperArm.localPosition = upperArmLocalPosition;
     forearm.localPosition = forearmLocalPosition;
     hand.localPosition = handLocalPosition;
     //Clean up temporary game objets.
     Destroy(upperArmAxisCorrection);
     Destroy(forearmAxisCorrection);
     Destroy(handAxisCorrection);
     //Transition.
     transition = Mathf.Clamp01(transition);
     upperArm.rotation = Quaternion.Slerp(upperArmRotation, upperArm.rotation, transition);
     forearm.rotation = Quaternion.Slerp(forearmRotation, forearm.rotation, transition);
     hand.rotation = Quaternion.Slerp(handRotation, hand.rotation, transition);
     //Debug.
     if (debug){
         Debug.DrawLine(forearm.position, elbowTarget.position, Color.yellow);
         Debug.DrawLine(upperArm.position, target.position, Color.red);
     }
     }
 }


Comment
Add comment · Show 1 · 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 JuanseCoello · Dec 22, 2014 at 01:10 PM 0
Share

thanks!!!!

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

28 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

Related Questions

Multiple Cars not working 1 Answer

JS to C# conversion Problem 1 Answer

Need help with picking up items by typing "E" 0 Answers

Movement script not working on imported model 3 Answers

Saving a value if it is larger than the current value... 3 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