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 /
avatar image
0
Question by breadmaker · Dec 07, 2010 at 10:46 AM · movementcharacterlimbs

character's limb movement restrictions

Hello everybody, new Unity user here.

I have a simple character to which I assigned different keys to move different parts of the body: R for left shoulder, E for left elbow, etc, at the click of the mouse. How can I restrict those movements to keep them realistic? I don't want the knee or elbow to bend beyond what's supposed to bend. Here's the code I'm trying to tweak. It's for the left hip. Many thanks.

var smooth:int;

private var targetPosition:Vector3;

function Update () { if(Input.GetKeyDown(KeyCode.F)) { var playerPlane = new Plane(Vector3.left, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0;

     if (playerPlane.Raycast (ray, hitdist)) {
         var targetPoint = ray.GetPoint(hitdist);
         targetPosition = ray.GetPoint(hitdist);
         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
         transform.rotation = targetRotation;
     }
 }

 transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);

}

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
1
Best Answer

Answer by skovacs1 · Dec 07, 2010 at 05:23 PM

There are a couple of ways to go about this:

  • Easy. Use rigidbodies and setup joints to constrain the movement. Rigidbodies enable the physics system to do all of the work for you. By changing collisions, etc. and/or removing the rigidbodies when you don't need them, this system can be done with a bit less overhead. I recommend CharacterJoints. There's also a ragdoll wizard that will set your joints up for you if your object is setup correctly.
  • Medium. It seems a bit roundabout, but you could create animations on different layers for the different movements and then play the different animations a certain duration based on your input. The advantages of this implementation is that you only need to change your animations to change your restrictions and movements and this keeps you away from any sticky math. This does not however give the most natural impression without having to do some solving.
  • Medium-Hard. Constrain the values numerically (clamping localRotation or something of the sort). This will require a lot of testing and tuning as well as a fair amount of adjustment to the above code and your setup because simply Lerping one part to some mouse position will not adjust the connected parts and you'd still need to constrain both position and rotation. If you set the pivots for all of the objects to be at the point where they join their parent, then you only need to concern yourself with rotation of the joints and can ignore positions for the most part. If you want positional control (such as drag the hand to this point) with this implementation, you may have to write your own IK solver to determine the rotations to use to make that happen.

Some notes about your code:

  • Why have targetPoint and targetPosition when they're the same?
  • Why are you storing targetRotation when you never use it after the next line? If you did something else with it, this would make sense, but with the posted code, you may as well just assign your quaternion directly.
  • You actually don't need to calculate the look rotation in this case as there is a LookAt function.
  • You're checking a world-axis-left facing plane, not a camera-aligned or transform-relative oriented plane. This may not be wrong depending on your use case, but I feel it is worth mentioning to identify unexpected behaviour that this may cause if your setup is not expecting it to be world-aligned.
  • Your movement will only smooth as it approaches the target (is this desired behaviour?) and it is very framerate dependent. You might consider moving the lerp code into FixedUpdate and simply lerping by a constant fraction.

This is a shorter and clearer version of your code:

var smooth:float; private var target : Vector3;

function Update () { if(Input.GetKeyDown(KeyCode.F)) { var playerPlane = new Plane(Vector3.left, transform.position); var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hitdist = 0.0;

     if (playerPlane.Raycast (ray, hitdist)) {
         target = ray.GetPoint(hitdist);
         transform.LookAt(target);
     }
 }
 transform.position = Vector3.Lerp(transform.position,target,
                                   Time.deltaTime*smooth);

}

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 breadmaker · Dec 08, 2010 at 02:31 AM 0
Share

Hola skovaks1, Thank you so much for your help. The edits you made improved the entire animation. The reason you saw so many unnecessary lines is because I got this script from the Unify Community website and was making a sorry attempt at customizing it.

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

No one has followed this question yet.

Related Questions

Platformer character movement 0 Answers

How would I make a character flatten against a wall? 0 Answers

How to handle walls with my FirstPersonCharacter? 1 Answer

How to prevent rigidbody with capsule collider from walking off the edge of cube mesh if button held down? 1 Answer

Non-symmetrical Character walk left animation. 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