• 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 05, 2012 at 06:37 AM · rotation

Script Problem

I made an updated version of the script that was on my old post. Now all it makes the flashlight do is rotate in a circle around the x axis. the flashlight is supposed to go from (0, 0, 0) to (31, 0, 0) when sprinting, and back to (0, 0, 0) when not sprinting. Please tell me what i did wrong... again...

 var rotationX = 0.0;
 var sprintActive : boolean = false;
 var breathingObj : GameObject;
 var lightObject : Light;
 var stopMovement : boolean = false;
 
 function Update () {
 
 rotationX = lightObject.transform.rotation.x;
 
  if(sprintActive == true && stopMovement == false){
 
  lightObject.transform.Rotate(Time.deltaTime * 120, 0, 0);
  
  }
  
  else if(sprintActive == false && stopMovement == false){
 
  lightObject.transform.Rotate(Time.deltaTime * -120, 0, 0);
  
  }
  
  if(rotationX > 31 && sprintActive == true){
  
  stopMovement = true;
  
  }
  
  if(rotationX < 0 && sprintActive == false){
  
  stopMovement = true;
  
  }
 }
 
 function Start(){
 
 stopMovement = true;
 
  while(true){
 
  yield WaitForSeconds(0.1);
 
  if(breathingObj.GetComponent(SprintScript).isSprinting == true){
 
  sprintActive = true;
  
  stopMovement = false;
 
  }
 
  else if(breathingObj.GetComponent(SprintScript).isSprinting == false){
 
  sprintActive = false;
  
  stopMovement = false;
  
  }
  }
 }
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 ScroodgeM · Aug 05, 2012 at 11:11 AM

rotationX = lightObject.transform.rotation.x;

this line is totally incorrect if you don't understand Quaternion's internal mechanism.

instead of all these check you can do simple thing (handwrited C#-like code)

Quaternion targetRotation = Quaternion.Euler(sprintActive ? 31f : 0f, 0f, 0f);

this will make a rotation that your object should rotate to, rotation is already based on sprintActive value.

transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 10f * Time.deltaTime)

and this will rotate your object to target rotation at maximum speed 10 deg/sec.

so, finally your Update() method will looks like:

void Update()
{
    Quaternion targetRotation = Quaternion.Euler(sprintActive ? 31f : 0f, 0f, 0f);
    lightObject.transform.rotation = Quaternion.RotateTowards(lightObject.transform.rotation, targetRotation, 10f * Time.deltaTime)
}

and 'stopMovement' is no need actually now cause object will not rotate more then needed. unless you want to stop rotating immediately before gaining target rotation...

Comment
Add comment · Show 4 · 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 06, 2012 at 04:49 AM 0
Share

What does the "?" do?

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

Also, What would the javascript version of:

void Update() { Quaternion targetRotation = Quaternion.Euler(sprintActive ? 31f : 0f, 0f, 0f); lightObject.transform.rotation = Quaternion.RotateTowards(lightObject.transform.rotation, targetRotation, 10f * Time.deltaTime) }

be?

avatar image DNP · Aug 06, 2012 at 04:52 AM 0
Share

Im really confused about this please explain a bit about how this works if you wouldnt $$anonymous$$d

avatar image ScroodgeM · Aug 06, 2012 at 07:41 AM 0
Share

'?' is a condition symbol.

if (a > 0)
{
    b = 1;
}
else
{
    b = 0;
}

can be writed as

b = a > 0 ? 1 : 0;

about JS: i don't write in JS. it's not a lot difference between C# and JS in unity.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Bullet not moving 1 Answer

What is wrong with this script? 2 Answers

Make player unable to shoot when reloading 3 Answers

Main Menu Problem 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