Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jonhillman · Jul 29, 2011 at 09:32 PM · javascriptonmousedrag

slingshot style mouse mechanic, don't want inverted when player facing left

For a 2D game (movement on x/y only) I have a 'slingshot' style moving mechanic in progress where you click on the player and drag behind him, then release to propel forward. During the drag, the angle the player faces is opposite the mouse position (see code below). I have this close, but have hit a wall...when the player flips to look left due to the mouse being dragged to the right of the player center, the y-axis mouse movement is being used in the opposite direction, so that up=up, as opposed to up=down as it is when the player is facing right.

This is what I have right now:

 function OnMouseDrag(){
     zCurrent = transform.rotation;
     mousepos = Input.mousePosition;
     mousepos.z = Input.mousePosition.z; //The distance between the camera and object, is flattened here
     var objectpos = Camera.main.WorldToScreenPoint(transform.position);
     mousepos.x = (mousepos.x - objectpos.x);
     mousepos.y = (mousepos.y - objectpos.y);
     mouseAngle = Mathf.Atan2(mousepos.y, mousepos.x) * Mathf.Rad2Deg;
     angleFlip = (mouseAngle - 180);
     
     if(((angleFlip+360)>90)&&((angleFlip+360)<270)){
         angleFlip = mouseAngle;
         var y : float = 180.0;
         Debug.Log("rotated to face left");
     }
     else{
         y = 0.0;
         Debug.Log("rotated to face right");
     }
     zTarget = Quaternion.Euler(Vector3(zCurrent.x, y, angleFlip));
     //zTarget = Quaternion.Euler(Vector3(zCurrent.x, zCurrent.y, angleFlip));
     ChangeDirection(zCurrent, zTarget);

And...

 function ChangeDirection(zCurrent : Quaternion, zTarget : Quaternion){
     // Dampen towards the target rotation
     var pointInTime : float = 0.0;
     while (pointInTime <= rotationDamping) {
         transform.localRotation = Quaternion.Slerp(zCurrent, zTarget, pointInTime/rotationDamping);
         pointInTime += Time.deltaTime;
         yield 0;
     }
 }

What can I do to keep the behavior the same as when facing right while facing left? (so that the player is rotated to 'look' opposite the mouse position)

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 SilverTabby · Jul 30, 2011 at 03:41 AM

Your problem is in this line:

 angleFlip = (mouseAngle - 180);

You are moving your angle across both the X && the Y axis by just subtracting by 180. You need to flip across the X axis, but not the Y axis for the result you want

you can fix this in either of two ways:

 angleFlip = 180 - mouseAngle; //flips across X, but not Y

or the more descriptive but less efficent

 angleFlip = Mathf.Atan2(mousepos.y, -mousepos.x) * Mathf.Rad2Deg;
Comment
Add comment · Show 3 · 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 jonhillman · Aug 01, 2011 at 07:15 PM 0
Share

Thanks for the reply! While that suggestion did not work (and I had tried that before), it made me rethink how I was rotating, and I came up with a new scheme that does work. Basically, I'm rotating a child object within a new 'master' object now, and that made this easier to deal with.

avatar image SilverTabby · Aug 01, 2011 at 07:20 PM 0
Share

I somehow knew clever parenting would solve your problem.

Though I'm confused why this wouldn't work, I actually did a bit of algebra to get to the angle - 180 so I would like to know where my mistake is, if any :\

avatar image SilverTabby · Aug 01, 2011 at 07:30 PM 0
Share

I think I found it. This line:

 if(((angleFlip+360)>90)&&((angleFlip+360)<270))

makes absolutely no sense :|

The first condition will ALWAYS be true because the range of atan (converted to degrees) is [180, -180] + 360 = [540, 180]

and the second condition will only be true if angle flip is between [-180, -90) + 360 = [180, 270)

I don't think it was my math, but the fact that the script never actually uses it. Ins$$anonymous$$d of using +360, just try $$anonymous$$athf.abs(). Then the if will be true when the angle is between (-270, -90) U (90, 270)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why OnMouseDrag is not being called? 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How can I stop OnMouseDrag from overriding a hinge joint's attachment? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Webplayer String length fails? 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