I want to rotate a Game Object[2] according to another Game Object[1]. I am successfully rotating Game Object[1]. But, I want that wherever my Game Object[1] rotate, my another game object must rotate there. I mean if, my Game Object[1], will rotate left then, the my Game Object[2], must go left side.
void Update(){
if(Physics.Raycast(mainCamera.camera.ScreenPointToRay(Input.mousePosition), out rayCastHit)){
if(rayCastHit.collider.tag =="sphere1"){
print(rayCastHit.collider.gameObject);
Demo();
}
}
}
void Demo(){
if(Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Moved )
{
if(deltaPosition.x == -1 && deltaPosition.y == -1)
deltaPosition = Input.GetTouch(0).position;
afterDeltaPosition = Input.GetTouch(0).position;
angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ;
if(angle < 45f && angle > -45f)
{
log.text = "right";
yRotation += Time.deltaTime*2;
Globe.rigidbody.AddTorque(Vector3.down*1000, ForceMode.Acceleration);
}
else if(angle < 135f && angle > 45f)
{
xRotation += Time.deltaTime*2;
log.text = "up";
Globe.rigidbody.AddTorque(Vector3.right*1000, ForceMode.Acceleration);
}
else if(angle > 135f || angle < -135f)
{
log.text = "left";
yRotation -= Time.deltaTime*2;
Globe.rigidbody.AddTorque(Vector3.up*1000, ForceMode.Acceleration);
}
else if(angle > -135f && angle < -45f)
{
log.text = "down";
xRotation -= Time.deltaTime*2;
Globe.rigidbody.AddTorque(Vector3.left*1000, ForceMode.Acceleration);
}
}
else if( Input.touchCount == 0)
{
if(deltaPosition.x != -1 && deltaPosition.y != -1)
{
deltaPosition.x=-1;
deltaPosition.y=-1;
}
}
}
You Probably didn’t found the correct angle,
so try this GameObject create cube not sphere so you will know it will rotate when you mouse down and attach this script.
public class Object1 : MonoBehaviour {
float distance;
Vector3 mousePos;
Vector3 relative;
float angle;
// Use this for initialization
void Start () {
distance = Vector3.Distance(transform.position, Camera.main.transform.position);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance));
relative = transform.InverseTransformPoint(mousePos);
angle = Mathf.Atan2(relative.y, relative.x) * Mathf.Rad2Deg;
if (angle < 0f) angle += 360f;
transform.Rotate(0, 0, angle);
print("angle " + angle);
if(angle < /*findByYourSelf*/)
{
// rotate Object2 to the Left
}
}
}
}
Edit: You would like also adjust your torque parameter in the inspector while you Play the game, and to make those things easier add this parameters, because addTorque need to be called not just one time.
public float angDrag;
public float spinForce;
public float maxAngVel;
void FixedUpdate()
{
rigidbody.angularDrag = angDrag;
rigidbody.maxAngularVelocity = maxAngVel;
rigidbody.AddTorque(0,10 * spinForce,0);
if (Input.GetMouseButtonDown(0))
{
this.rigidbody.AddForceAtPosition(Vector3.right * 1000, transform.position);
//this.rigidbody.AddTorque(Vector3.left * 20000);
}
}
I got the solution. So I am posting it here…
void Update(){
if(Physics.Raycast(mainCamera.camera.ScreenPointToRay(Input.mousePosition), out rayCastHit)){
if(rayCastHit.collider.tag =="sphere1"){
print(rayCastHit.collider.gameObject);
Demo();
}
}
}
void Demo(){
if(Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Moved )
{
if(deltaPosition.x == -1 && deltaPosition.y == -1)
deltaPosition = Input.GetTouch(0).position;
afterDeltaPosition = Input.GetTouch(0).position;
angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ;
if(angle < 45f && angle > -45f)
{
log.text = "right";
yRotation += Time.deltaTime*2;
Globe.rigidbody.AddTorque(Vector3.down*1000, ForceMode.Acceleration);
rotatingCube.transform.Rotate(0,yRotation,0);
}
else if(angle < 135f && angle > 45f)
{
xRotation += Time.deltaTime*2;
log.text = "up";
Globe.rigidbody.AddTorque(Vector3.right*1000, ForceMode.Acceleration);
rotatingCube.transform.Rotate(xRotation,0,0);
}
else if(angle > 135f || angle < -135f)
{
log.text = "left";
yRotation -= Time.deltaTime*2;
Globe.rigidbody.AddTorque(Vector3.up*1000, ForceMode.Acceleration);
rotatingCube.transform.Rotate(0,yRotation,0);
}
else if(angle > -135f && angle < -45f)
{
log.text = "down";
xRotation -= Time.deltaTime*2;
Globe.rigidbody.AddTorque(Vector3.left*1000, ForceMode.Acceleration);
rotatingCube.transform.Rotate(xRotation,0,0);
}
}
else if( Input.touchCount == 0)
{
if(deltaPosition.x != -1 && deltaPosition.y != -1)
{
deltaPosition.x=-1;
deltaPosition.y=-1;
}
}
}