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 Presipope · Nov 22, 2012 at 02:28 AM · sidescroller

Trying to get character to instantly rotate in 2.5d side scroller

So I need the character to "flip" sides whenever the mouse is on the left or right of the screen. I'd like to say I'm very new to scripting and only know the basics, which is part of my problem. I figured if I could right a true false statement that involved the Mouse X input then I could just flip the object whenever the mouse x value was below 0. The error comes in the line where I'm trying to do the rotation.

Hopefully I'm making some sense

 var lookForward : boolean = true;
 
 function Update () {
  
      if(Input.GetAxis("Mouse X") < 0) {
         
         lookForward = false;
      
      }
      
      if(!lookForward) {
      
          transform.Rotate.Vector3(0, -90, 0 * Time.deltaTime);
          
          }
 
 }
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
3
Best Answer

Answer by aldonaletto · Nov 22, 2012 at 03:05 AM

transform.Rotate.Vector3 doesn't exist: you should write transform.Rotate(Vector3(...));
Anyway, the code you're using turns the character -90 degrees each frame the mouse is moving to the right. In order to flip the character left/right when the mouse is on the left/right sides of the screen, use this:

 private var lookLeft: Quaternion;
 private var lookRight: Quaternion;
 
 function Start(){
   // assuming character starts looking to the right:
   lookRight = transform.rotation;
   // calculate rotation flipped 180 degrees:
   lookLeft = lookRight * Quaternion.Euler(0, 180, 0); 
 }
 
 function Update(){
   // if mouse is on the left side of the screen...
   if (Input.mousePosition.x < Screen.width/2){
     transform.rotation = lookLeft; // look left
   } else {
     transform.rotation = lookRight; // look right
   }
 }
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 Presipope · Nov 22, 2012 at 03:48 AM 0
Share

Thanks a bunch! I actually figured it out and got it working I was so proud looking up that input mouse position and dividing the screen width by 2! In all seriousness I wrote a function similar to yours, although I think yours is definitely more efficient. Thanks again.

avatar image _Shockwave · Sep 02, 2013 at 01:40 PM 0
Share

this worked for me too. Thanks!

avatar image KurtRussellsMullet · Apr 13, 2015 at 02:34 AM 0
Share

Worked for me as well. Looked for this forever! Thanks you very much!

avatar image Slater7 · Sep 29, 2020 at 08:48 PM 0
Share

Thanks alot aldonaletto!

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

12 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

Related Questions

how to do 2.5D side scroller-Gun point towards mouse then -180 character turn 1 Answer

Side Scroller Script, Need help 1 Answer

character turning around. 3 Answers

Any good NEW 2D side scrolling tutorials? -1 Answers

how to get a camera to side scroll 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