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 /
This question was closed Nov 22, 2015 at 02:25 AM by graiderzs for the following reason:

Other, I solved it.

avatar image
0
Question by graiderzs · Sep 30, 2015 at 07:09 PM · c#gameobjectrotate

2D When rotating an object around a pivot, when mouse click, how do I prevent it from moving to the mouse position immediately?

Hello Everyone,

In my game, I am rotating an object around a pivot. When my mouse move clockwise, the gameobject will also move clockwise.

However, when I mouse click and move, the object immediately move to the mouse position.

What I want is, when I mouse click and move, the object does not move immediately to the mouse position, and will move clockwise or anti-clockwise, from that spot.

This is the code,

using UnityEngine; using System.Collections;

public class playerController : MonoBehaviour {

 public Transform target; 
 public float fRadius = 3.0f;
 public float deltaAngle;
 public Vector3 startTransform;
 public Quaternion endRotation;
 public Vector3 touchPoint;
 public float ang;
 public float angleDifference;
 public Vector3 v;

void Update () {

     if (Input.GetMouseButtonUp(0))
     {
         endRotation.z = transform.eulerAngles.z;

     }
     if (Input.GetMouseButtonDown(0))
     {

         touchPoint = Camera.main.WorldToScreenPoint (target.position);
         touchPoint = Input.mousePosition - touchPoint;
         
         float dx = touchPoint.x - target.position.x;
         float dy = touchPoint.y - target.position.y;
         deltaAngle = Mathf.Atan2(dy,dx) * Mathf.Rad2Deg;;

         startTransform = transform.position;
     }

     if( (Input.GetAxis("Mouse X")<0) || (Input.GetAxis("Mouse X")>0) )
     {
         
         Vector3 pt = Camera.main.WorldToScreenPoint (target.position);
         pt = Input.mousePosition - pt;

         float dx = pt.x - target.position.x;
         float dy = pt.y - target.position.y;                
         ang = Mathf.Atan2(dy,dx) * Mathf.Rad2Deg;;

         angleDifference = deltaAngle - ang;

         transform.rotation = Quaternion.Euler (0,0,endRotation.z - angleDifference);


         v = Quaternion.AngleAxis (-angleDifference, Vector3.forward) * (Vector3.right * fRadius);
         transform.position = target.position - v;
     }
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

  • Sort: 
avatar image
0

Answer by graiderzs · Oct 11, 2015 at 11:21 AM

Okay, great news! I got it to work, after countless hours.

This is the code, for anyone who wants to do the same as me. ^.^

using UnityEngine; using System.Collections;

public class playerController : MonoBehaviour {

 public Transform target; 
 public float fRadius = 3.0f;

 public bool canStart;
 public bool canPlay;

 public float deltaAngle;
 public float startRotationZ;
 public Quaternion endRotation;
 Vector3 touchPoint;
 public float ang;
 public float angleDifference;
 public float lastAngledifference;
 public Vector3 v;
 private float dx;
 private float dy;
 private Vector3 pt;

 void Start ()
 {
     startRotationZ = transform.eulerAngles.z;
 }


 void Update () {

     if(Input.GetMouseButtonDown(0))
     {
         angleDifference = 0;
     }

     //Controls
     //If never touch, stop moving
     if (!Input.GetMouseButton(0))
     {
         canPlay = false;
     }

     //If touch, can move
     if (Input.GetMouseButton(0))
     {
         canPlay = true;
     }

     if (Input.GetMouseButtonUp(0))
     {
         endRotation.z = transform.eulerAngles.z;

         lastAngledifference = lastAngledifference + angleDifference;

     }

     //The rotation
     if (canPlay)
     {
         if ( (Input.GetMouseButtonDown(0))  )
         {
             //mouse position at start, and calculate angle
             touchPoint = Camera.main.WorldToScreenPoint (target.position);
             touchPoint = Input.mousePosition - touchPoint;

             deltaAngle = Mathf.Atan2(touchPoint.x,touchPoint.y) * Mathf.Rad2Deg;

         }

         if( ( (Input.GetAxis("Mouse X")<0) || (Input.GetAxis("Mouse X")>0) )  )
         {

             //mouse position when moving, and calculate angle
             pt = Camera.main.WorldToScreenPoint (target.position);
             pt = Input.mousePosition - pt;

             ang = Mathf.Atan2(pt.x,pt.y) * Mathf.Rad2Deg;

             angleDifference = deltaAngle - ang;

             v = Quaternion.AngleAxis (lastAngledifference + angleDifference, Vector3.forward) * (Vector3.right * fRadius);

             transform.position = target.position - v;

         }
     }
 }

}

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 Le-Pampelmuse · Nov 20, 2015 at 08:54 AM 0
Share

If you have a solution you should close the question. Also, dont post it as a new answer to your question. You should convert it to a comment.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# Rotate GameObject at Other Point other than Center 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Rotate Object 180º OnClick 3 Answers

C# Rotate More than Two GameObjects 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