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 yoyoyaya · Jan 29, 2012 at 10:40 PM · rotationchildbody

lower and upper body rotation

Hello. Being fairly new to the world of coding, I am facing a problem.

I have the typical behavior of a human body to apply.

I have the upper body is a child of the lower body. it is on the lower body that my script is attached.

The upper body must turn to + 45 ° - 45 ° (min... an max.... var). If one tries to turn over, the entire body rotating at 45 ° (and so on). For the upper body (upperCube), I did this:

var sensitivity : float = 5; var xRotation : float; var lowerCubeRotation : float; var upperCube : GameObject; var minUpperCubeXRotation = -45; var maxUpperCubeXRotation = 45;

function Update () { var controller = GetComponent(CharacterController); xRotation += Input.GetAxis("Mouse X") * sensitivity; Rotation(); }

function Rotation() { if (xRotation > maxUpperCubeXRotation){ }

if (xRotation < minUpperCubeXRotation){ }

upperCube.transform.localEulerAngles = Vector3(0, xRotation, 0);

} So the upper body rotates and if it reaches one of the limits, the lower body must perform a rotation of 45 ° (var lowerCubeRotation) over a period of 3 seconds (to be defined in a var) and the upper body returns to allignment with the lower body.

But how to code the part of the lower body. As I said, I suck. I tested:

transform.Rotate(0, lowerCubeRotation, 0);

xRotation -= minUpperBodyXRotation;

which applies a rotation of 45 ° to transform (lower body), but instantly. And a rotation of the upper body to 0 (at the bottom allignment).

My problem lies in the duration of the rotation. I want it to end after x seconds, not immediately.

need help please. (post on unity forum)


use google translate

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jan 30, 2012 at 12:03 AM

You're in the right path: use euler angles to set the rotation, and always set x, y and z at the same time. You can use Mathf.MoveTowards to move only the angle y at constant speed (degrees per second): save the initial lower part euler angles, and when the upper part goes out of the limits, add the upper rotation to the initial lower part y angle, then set the new target angle.

var sensitivity : float = 5; var xRotation : float; var lowerCubeRotation : float; var upperCube : GameObject; var minUpperCubeXRotation = -45; var maxUpperCubeXRotation = 45; var speed: float = 180; // degrees per second

private var targetAngle: float; // angle around y to reach private var curAngle: float; // current angle around y private var iniAngle: Vector3; // initial complete euler angles private var controller: CharacterController;

function Start(){ controller = GetComponent(CharacterController); iniAngle = transform.eulerAngles; // save the complete initial euler angles targetAngle = iniAngle.y; // initialize the target... curAngle = iniAngle.y; // and the current Y angles }

function Update () { xRotation += Input.GetAxis("Mouse X") * sensitivity; Rotation(); }

function Rotation() { // set the upper part rotation (use modulo 360 to avoid crazy rotations): upperCube.transform.localEulerAngles = Vector3(0, xRotation % 360, 0); if (xRotation > maxUpperCubeXRotation || xRotation < minUpperCubeXRotation){ // if xRotation gone out of limits... targetAngle = iniAngle.y + xRotation; // find the new target angle } // "move" the current angle gradually each update... curAngle = Mathf.MoveTowards(curAngle, targetAngle, speed * Time.deltaTime); // keep the original x and z angles, and set the current angle modulo 360 transform.eulerAngles = Vector3(iniAngle.x, curAngle % 360, iniAngle.z); }

Comment
Add comment · 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
0

Answer by yoyoyaya · Jan 30, 2012 at 10:00 PM

Thank you for the answer. I used some of what you said for this:

var sensitivity : float = 5; var xRotation : float; var upperBodyXRotation : float; var speed: float = 180; var upperBody : GameObject; var minUpperBodyXRotation : float; var maxUpperBodyXRotation : float;

private var transformTargetAngle : float; private var transformCurrentAngle : float; private var transformInitialAngle : Vector3;

function Start() { controller = GetComponent(CharacterController); transformInitialAngle = transform.eulerAngles; transformTargetAngle = transformInitialAngle.y; transformCurrentAngle = transformInitialAngle.y; }

function Update () { xRotation += Input.GetAxis("Mouse X") * sensitivity; upperBodyXRotation = xRotation; if (upperBodyXRotation > maxUpperBodyXRotation) { upperBodyXRotation = maxUpperBodyXRotation; } if (transform.eulerAngles.y == transformTargetAngle) { transformInitialAngle = transform.eulerAngles; } Rotation(); }

function Rotation() { if (upperBodyXRotation == maxUpperBodyXRotation) { upperBodyXRotation -= maxUpperBodyXRotation; transformTargetAngle = transformInitialAngle.y + maxUpperBodyXRotation; transformCurrentAngle = Mathf.MoveTowards(transformCurrentAngle, transformTargetAngle, speed * Time.deltaTime); transform.eulerAngles = Vector3(transformInitialAngle.x, transformCurrentAngle % 360, transformInitialAngle.z);

 }
 upperBody.transform.eulerAngles = Vector3(0, xRotation % 360, 0);

}

in this case, when the upper body reaches 45 ° (maxUpperBodyXRotation), it runs the function that turns the lower body 45 degrees. The upper body is found at 0 ° with:

upperBodyXRotation -= maxUpperBodyXRotation;

logically, when I start again to turn the upper body 45 °, rotation of the lower basin should turn 45 degrees but it does not. "upperBodyXRotation" reste à 0.

I think for the rotation of the lower body, just change the "transform.eulerAngles" of "transformInitialAngle = transform.eulerAngles" by the new "transform.eulerAngles" from rotation function.

I tested this to reassign "transform.eulerAngles" with that obtained in the rotation function but it does not work:

if (transform.eulerAngles.y == transformTargetAngle) {
    transformInitialAngle = transform.eulerAngles;
}

So to summarize, at present, I can rotate that lasts x seconds at the bottom of the body when the upper body is "maxUpperBodyXRotation" but this only works once. How can it be repeated?

I want that if the upper body rotates at an angle of 45 ° (maxUpperBodyXRotation) from the lower body, lower body rotates 45 ° ("maxUpperBodyXRotation" or other). Thus, the upper body is at the same rotation as the lower body (0, allignment upper and lower body) and subsequently, the action can be repeated.

May I ask a lot. But from the world of 3D, I know very little the world of the script. I learn every day a little more but sometimes I stuck. So if someone can give me his help, thank you.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating Biped's Upper Body seperately from the Lower Body 1 Answer

Why is a child object not rotating properly with parent? 1 Answer

Child affecting parent rigidbody? 1 Answer

Child world location 3 Answers

Neutralising child rotations -> Parent 1 Answer


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