Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Vurtigo · Oct 21, 2018 at 12:31 AM · rotationaddforcecirclewheelmousedrag

Adding force to a rotating wheel using mouse or finger drag.

I have a wheel that I am currently try to add a force or torque to after a mouse/finger drag. Think of it as a wheel of fortune that can be spun in any direction. I have six nodes on the outside of my object with colliders. When these colliders are grabbed, you can rotate the wheel. The problem I am having is having the force added in the direction of the swipe. I can grab the wheel and rotate it and add the force once I let go. However the force only adds in one direction. I would like for the wheel to rotate in all directions. I feel like I'm missing something very simple here but I've had this problem for days now and I can't find a solution myself. Here is the code I am using, sorry if it's a bit messy! I have been wracking my brain around the place trying to make this work!

using System.Collections; using System.Collections.Generic; using UnityEngine; public class rotation : MonoBehaviour { private float angle = 0.1f; Rigidbody rb; Vector3 startPos; Vector3 endPos; float startX; float deltaX; float touchSpeed; Vector3 Torque; float timeStart; float timeEnd; // Use this for initialization void Start() { rb = GetComponent<Rigidbody>(); //angle1 = transform.eulerAngles.z; } // Update is called once per frame void Update() { } void OnMouseOver() { Vector3 pos = Camera.main.WorldToScreenPoint(transform.position); pos = Input.mousePosition - pos; angle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg; angle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg; //Debug.Log("Angle is " + angle); } void OnMouseDrag() { RotateObj(); } void RotateObj() { Vector3 pos = Camera.main.WorldToScreenPoint(transform.position); pos = Input.mousePosition - pos; float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - angle; transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward); // Debug.Log("Angle is " + ang); } /*void RotateTo(float ang) { Quaternion newAng = Quaternion.identity; newAng.eulerAngles = new Vector3(0, 0, ang); transform.rotation = Quaternion.Slerp(transform.rotation, newAng, Time.time * turnSpeed / 30); }*/ void OnMouseDown() { startPos = Input.mousePosition; timeStart = Time.time; Debug.Log("start is " + startPos); } void OnMouseUp() { endPos = Input.mousePosition; Vector3 deltaPos = endPos - startPos; Debug.Log("end is " + endPos); timeEnd = Time.time; touchSpeed = deltaPos.magnitude / (timeEnd - timeStart); Torque = new Vector3(0, 0, touchSpeed * 1); rb.AddTorque(Torque, ForceMode.Force); } }

Comment
Add comment · Show 2
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 Vurtigo · Oct 21, 2018 at 12:39 AM 0
Share

Apologies for the mess of the code. Not sure why it displays like that. I tried to change it numerous times to no avail. If you look in the comments the code shows neater and the way it's supposed to be.

avatar image Vurtigo · Oct 21, 2018 at 12:40 AM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class rotation : $$anonymous$$onoBehaviour
 {
 
     private float angle = 0.1f;
 
     Rigidbody rb;
     Vector3 startPos;
     Vector3 endPos;
     float startX;
     float deltaX;
 
     float touchSpeed;
     
     Vector3 Torque;
     float timeStart;
     float timeEnd;
 
     // Use this for initialization
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         //angle1 = transform.eulerAngles.z;
     }
 
     // Update is called once per frame
     void Update()
     {
      
     }
 
     void On$$anonymous$$ouseOver()
     {
 
         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
         pos = Input.mousePosition - pos;
         angle = $$anonymous$$athf.Atan2(pos.y, pos.x) * $$anonymous$$athf.Rad2Deg;
         angle -= $$anonymous$$athf.Atan2(transform.right.y, transform.right.x) * $$anonymous$$athf.Rad2Deg;
         //Debug.Log("Angle is " + angle);
     }
 
     void On$$anonymous$$ouseDrag()
     {
         RotateObj();
     }
 
     void RotateObj()
     {
         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
         pos = Input.mousePosition - pos;
 
         float ang = $$anonymous$$athf.Atan2(pos.y, pos.x) * $$anonymous$$athf.Rad2Deg - angle;
         transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
 //      Debug.Log("Angle is " + ang);
     }
 
     /*void RotateTo(float ang)
     {
         Quaternion newAng = Quaternion.identity;
         newAng.eulerAngles = new Vector3(0, 0, ang);
         transform.rotation = Quaternion.Slerp(transform.rotation, newAng, Time.time * turnSpeed / 30);
         
     }*/
 
     void On$$anonymous$$ouseDown()
     {
         startPos = Input.mousePosition;
         timeStart = Time.time;
         Debug.Log("start is " + startPos);
 
     }
 
     void On$$anonymous$$ouseUp()
     {
         endPos = Input.mousePosition;
         Vector3 deltaPos = endPos - startPos;
         Debug.Log("end is " + endPos);
 
         timeEnd = Time.time;
         touchSpeed = deltaPos.magnitude / (timeEnd - timeStart);
 
         Torque = new Vector3(0, 0, touchSpeed * 1);
         rb.AddTorque(Torque, Force$$anonymous$$ode.Force);
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

187 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Applying AddForce() while rotating player gameobject. 0 Answers

Rotat a GameObject and than shoot it in the New (x) direction 1 Answer

Unity 2D jumping only up 0 Answers

Getting 2D Turrets to rotate toward bullet direction. 1 Answer

How do i find player rotation and convert that into a force facing that direction. 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