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 DNP · Aug 02, 2012 at 12:18 AM · rotationbuttonanglespressed

Rotation Code Help

Whats wrong with this code? Its supposed to make the object rotate to (31, 0, 0) when left shift is pressed and then when left shift is released it makes it go back to (0, 0, 0) rotation. Can someone help?

This isnt a question on the sprint script, its a question on the rotation. The rotation goes crazy when you press left shift.

 function Update(){
  if(Input.GetButtonDown("Sprint") && isSprinting == false && canRun == true){
  
  obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 8;
  
  lightObj.transform.eulerAngles = Vector3(31, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
  
  isSprinting = true;
  
  StopSprinting();
 
 }
 
 function StopSprinting(){
 
  yield WaitForSeconds (8);
 
  if(isSprinting == true){
  
  isSprinting = false;
  
  lightObj.transform.eulerAngles = Vector3(0, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
  
  audio.PlayOneShot(breathSound);
  
  audio.volume = 1;
  
  obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 6;
  
  canRun = false;
  
  yield WaitForSeconds (10);
  
  if(canRun == false){
  
  canRun = true;
  
  }
  }
 }

Please help!

Comment
Add comment · Show 4
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 Eric5h5 · Aug 02, 2012 at 01:49 AM 0
Share

If you want the angle to be (31, 0, 0), wouldn't you just write that? You seem to be using the y and x euler angles from another object.

avatar image midomido · Aug 02, 2012 at 01:55 AM 0
Share

Try creating a cube of rotation (31,0,0) and linking it to the script and using its rotation lightObj.transform.rotation = Cube.transform.rotation; and see if it works or not

avatar image DNP · Aug 02, 2012 at 02:51 AM 0
Share

I keep getting weird values every time it changes

avatar image DNP · Aug 02, 2012 at 11:47 PM 0
Share

I seem to have figured this out myself. I could just use

lightObj.transform.Rotate(31,0,0);

lightObj.transform.Rotate(-31,0,0);

But now im trying to make this a smooth rotation, so could you please go to my post and help as much as you can on that topic? because im nearly ripping my hear out because of smooth rotation.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 02, 2012 at 02:09 AM

You're swapping X and Z angles each time you rotate to 31 or return to 0:

  // the last eulerAngles component should be Z, not X:
  lightObj.transform.eulerAngles = Vector3(31, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
  ...
  // the same applies to this line:
  lightObj.transform.eulerAngles = Vector3(0, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
Change the last component to lightObj.transform.eulerAngles.z

EDITED: If you want to rotate smoothly to the new orientation, have a variable (lightAngle) to define the desired angle and follow it at Update with Lerp:

// declare these variables in your script: var lightVel: float = 5; // speed to rotate the lightObj private var curAngle: Vector3; // current lightObj local angle private var lightAngle: Vector3; // desired lightObj local angle

function Start(){ // initialize the new variables at Start: lightAngle = lightObj.localEulerAngles; curAngle = lightAngle; // other previous Start code }

function Update(){ if(Input.GetButtonDown("Sprint") && isSprinting == false && canRun == true){ obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 8; lightAngle.x = 31; // set the desired angle isSprinting = true; StopSprinting(); } // this code goes smoothly to the desired angle: curAngle = Vector3.Lerp(curAngle, lightAngle, lightVel * Time.deltaTime); lightObj.localEulerAngles = curAngle; // other previous Update code }

function StopSprinting(){ yield WaitForSeconds (8); if(isSprinting == true){ isSprinting = false; lightAngle.x = 0; // restore the original light angle audio.PlayOneShot(breathSound); audio.volume = 1; obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 6; canRun = false; yield WaitForSeconds (10); if(canRun == false){ canRun = true; } } }

Comment
Add comment · Show 6 · 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 DNP · Aug 02, 2012 at 02:37 AM 0
Share

it still doesnt seem to work. im getting values like "6.54845e-05"

avatar image Eric5h5 · Aug 02, 2012 at 02:56 AM 0
Share

6.54845e-05 is close to 0, and what you can expect with eulerAngles, given floating point precision issues.

avatar image aldonaletto · Aug 02, 2012 at 03:10 AM 0
Share

As @Eric5h5 said, if the angles Y and Z are always zero you could just assign the desired angles to the object rotation:

  lightObj.transform.eulerAngles = Vector3(31,0,0); 
  ...
  lightObj.transform.eulerAngles = Vector3(0,0,0);
  ...
avatar image DNP · Aug 02, 2012 at 04:17 AM 0
Share

Yeah but when i activate the rotation, it rotates to the right with that code. the light just really glitches up.

avatar image aldonaletto · Aug 02, 2012 at 01:24 PM 0
Share

The code you've posted (with the suggested corrections) should rotate down the lightObj 31 degrees instantly, wait 8 seconds and rotate it back instantly to 0 degrees, unless the original Update function does something else that nulls this behaviour - it seems you've not posted the complete Update function, because its closing bracket is missing.

Show more comments
avatar image
0

Answer by MKlegoman357 · Aug 02, 2012 at 11:10 PM

Try to use this:

lightObj.transform.localRotation.x = 31;

lightObj.transform.localRotation.x = 0;

or

lightObj.transform.localRotation = Quaternion.Euler(31, lightObj.transform.localRotation.y, lightObj.transform.localRotation.z);

lightObj.transform.localRotation = Quaternion.Euler(0, lightObj.transform.localRotation.y, lightObj.transform.localRotation.z);

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 DNP · Aug 02, 2012 at 11:47 PM 0
Share

I seem to have figured this out myself. I could just use

lightObj.transform.Rotate(31,0,0);

lightObj.transform.Rotate(-31,0,0);

But now im trying to make this a smooth rotation, so could you please go to my post and help as much as you can on that topic? because im nearly ripping my hear out because of smooth rotation.

avatar image
0

Answer by MKlegoman357 · Aug 03, 2012 at 08:27 AM

This should work:

var xRotation : float;

var defaultRotation : Quaternion;

var xRotationV : float;

var xRotationTime : float = 1;

function Start () {

defaultRotation = transform.localRotation;

xRotation = defaultRotation.x;

}

function Update() { lightObj.transform.rotation = Quaternion.Euler(Mathf.SmoothDamp(transform.localRotation.x, xRotation, xRotationV, xRotationTime), defaultRotation.y, defaultRotation.z);

if(Input.GetButtonDown("Sprint") && isSprinting == false && canRun == true){

obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 8;

isSprinting = true;

xRotation = 31;

StopSprinting();

} }

function StopSprinting() {

yield WaitForSeconds (8);

if(isSprinting == true){

isSprinting = false;

xRotation = 0;

audio.PlayOneShot(breathSound);

audio.volume = 1;

obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 6;

canRun = false;

yield WaitForSeconds (10);

if(canRun == false){

canRun = true;

} } }

If its not then try defaultRotation declair by your self in the inspector.

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 DNP · Aug 04, 2012 at 06:11 AM 0
Share

Okay, so when i play the game the flashlight seems to only point in the direction that it was pointing when the game started. even twhen i turn it still continues to stay at the original rotation/position it started in. Its attached to the First person controllers main camera, so it should move when i turn. but it doesnt.

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

10 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

Related Questions

Euler to Quaternion conversion returns unexpected result at exactly 180 degree rotation on Y axis 0 Answers

Object won't rotate 2 Answers

Limiting the rotation of an object 1 Answer

Unity GUI Button Options 0 Answers

Rotation losing precision 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