Hello everybody!
I´m attempting to make a 3D Rotating Platform that implement a system similar to a “Angular Spring”.
However, a Spring Joint (or any other joint) seems to not suit my need.
The platform must tend to the (0, 0, 0) Angle, and if something make it rotate in X or Z angle, a torque is applyed on the opposite “direction”.
The X / Z Rotation is caused by the weight of an object positioned on the platform, on its point.
Besides, I can control the Y Rotation (torque) sliding the mouse cursor along the screen in horizontal movement, and obviously, the object on the platform must rotate together.
My problems:
1: If I set a reasonable torque force, the platform “pull back” force is shown very slow. But if I increase the value, the Platform is shown overly rigid.
2: If I set the platform control to add Torque only on the Y Axis, all the 3 axes recieve a force.
Can someone help me??
Sorry if it seems a Noob Question
My Script is shown below:
using UnityEngine;
using System.Collections;
public class PlatformBehaviour : MonoBehaviour
{
private Rigidbody myRB;
private float force;
private float torqueX;
private float torqueZ;
private float torqueY;
private float multiplX;
private float multiplZ;
private float Yincrease;
private Vector3 actualInputPos;
private Vector3 prevInputPos;
private float horizDisplacement;
void Start()
{
myRB = this.gameObject.GetComponent<Rigidbody> ();
force = 3.0f;
torqueX = 0.0f;
torqueZ = 0.0f;
multiplX = 0.0f;
multiplZ = 0.0f;
Yincrease = 10000;
horizDisplacement = 0.0f;
}
void FixedUpdate()
{
Press ();
ControlTorque ();
ProccessInput ();
Release ();
}
private float GetRotation(float rot)
{
if (rot > 180.0f && rot < 360.0f)
return (360 - rot) * -1;
return rot;
}
private void ControlTorque()
{
if (myRB.transform.eulerAngles.x != 0)
{
multiplX = (GetRotation (myRB.transform.eulerAngles.x) / Mathf.Abs (GetRotation (myRB.transform.eulerAngles.x))); //This calculation is only to get "1" or "-1" to define positive or negative.
torqueX = (myRB.mass * force) * (multiplX * -1);
}
if (myRB.transform.eulerAngles.z != 0)
{
multiplZ = (GetRotation (myRB.transform.eulerAngles.z) / Mathf.Abs (GetRotation (myRB.transform.eulerAngles.z))); //This calculation is only to get "1" or "-1" to define positive or negative.
torqueZ = (myRB.mass * force) * (multiplZ * -1);
}
myRB.AddTorque (new Vector3(torqueX, 0.0f, torqueZ), ForceMode.Force);
}
private void Press()
{
if (Input.GetMouseButtonDown(0))
{
actualInputPos = Input.mousePosition;
prevInputPos = Input.mousePosition;
}
}
private void Release()
{
if (Input.GetMouseButtonUp(0))
{
actualInputPos = new Vector3();
prevInputPos = new Vector3();
}
}
private void ProccessInput()
{
if (Input.GetMouseButton (0))
{
actualInputPos = Input.mousePosition;
if (actualInputPos.x > prevInputPos.x)
{
horizDisplacement = actualInputPos.x - prevInputPos.x;
torqueY = horizDisplacement * Yincrease * -1;
}
else
{
if (actualInputPos.x < prevInputPos.x)
{
horizDisplacement = prevInputPos.x - actualInputPos.x;
torqueY = horizDisplacement * Yincrease;
}
}
prevInputPos = actualInputPos;
}
else
{
torqueY = 0;
}
myRB.AddTorque (new Vector3(0.0f, torqueY, 0.0f), ForceMode.Force);
}
}
My Rigidbody configuration:
Mass: 12000
Drag: 0
Angular Grag: 1
Use Gravity: No
Kinematic: No
Interpolate: none
Collision Detect: Discrete
Constraints:
Position: All 3
Rotation: None