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); } }
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.
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612180658im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
More problems with arrow shooting 0 Answers
Lack of accuracy while spawning an effect on a rotating sphere. 0 Answers
Gameobject consistently rotates to the right when applying force along transform.forward 0 Answers
Rotate AddForce around a point? 0 Answers
How Calculation a new rotate on circle2d when add torque 0 Answers