2

I want to rotate my 3d model based on player touch on x and y direction. So that I am detecting horizontal and vertical touch.

But in this I want to restrict z direction rotation. For this I tried multiple codes and ask suggestions in other forums too. At present no suggestion is working for me.

Basically, I don't want z direction rotation. Following image give you more idea, where things going wrong. Model rotated completely into z direction. I want to stop this.

enter image description here

Here is my multiple tries to achieve same thing.

void Update ()
{
    // If there are two touches on the device...
    if (Input.touchCount == 1 && GameManager.Instance.IsGameStart) {

        // Store currnet touch.
        Touch touch = Input.GetTouch (0);

        //   transform.RotateAround (transform.position, Vector3.up, -     touch.deltaPosition.x  Time.deltaTime  15f);
        //   transform.RotateAround (transform.position, Vector3.right, touch.deltaPosition.y  Time.deltaTime  15f);
        //   transform.RotateAround (transform.position, Vector3.forward, 0f);

        //   transform.Rotate (Vector3.up, -touch.deltaPosition.x  Time.deltaTime  10f, Space.World);
        //   transform.Rotate (Vector3.right, touch.deltaPosition.y  Time.deltaTime  5f, Space.World);
        //   transform.Rotate (Vector3.forward, 0f, Space.World);

        myRigidbody.MoveRotation (myRigidbody.rotation  Quaternion.Euler (Vector3.right  touch.deltaPosition.y  Time.deltaTime  5f));
        myRigidbody.MoveRotation (myRigidbody.rotation  Quaternion.Euler (Vector3.up  -touch.deltaPosition.x  Time.deltaTime  10f));

    }
}

In above each block represent unique effort to restrict z direction. Now please give me some suggestion to achieve same thing.

As well my discussion running at Unity forum Touch based rotation of 3d model

EDIT: I have tried with restricting rigidbody in z rotation. So my inspector look something like this.

enter image description here

EDIT : After discussion on game development chat room. I have following kind of code :

float prevZ = transform.eulerAngles.z;
transform.Rotate (Vector3.up, -touch.deltaPosition.x  Time.deltaTime  10f, Space.World);
transform.Rotate (Vector3.right, touch.deltaPosition.y  Time.deltaTime  5f, Space.World);

Vector3 modelVec = transform.eulerAngles;
modelVec.z = prevZ;
transform.eulerAngles = modelVec;

I am just near to solution but now my golf globe model can't able to move more that 180 degree from top or bottom side drag. I just reach around 180 degree and it gets just rotated towards any other direction.

Serlite
  • 12,130
  • 5
  • 38
  • 49
Siddharth
  • 4,142
  • 9
  • 44
  • 90

2 Answers2

0

Ok after your comments, it sounds like you may need to start fresh.

Based on what I understand, try this out:

Vector3 rotation = new Vector3();
rotation.y = -touch.deltaPosition.x * Time.deltaTime * yRotSpeed;
rotation.x = touch.deltaPosition.y * Time.deltaTime * xRotSpeed;
rotation.z = 0;
transform.Rotate(rotation);

and then I'd suggest adding:

public float yRotSpeed;
public float xRotSpeed;

as fields of your script so that you can adjust the rotating speed within the unity engine in the future.

Adam B
  • 3,662
  • 2
  • 24
  • 33
0

Just do like this.

 // private variables
private Vector3 inputRotation;      // difference of input mouse pos and screen mid point
private Vector3 mouseinput;

// Update is called once per frame
void Update() {
    FindPlayerInput();
}
   void FindPlayerInput()
    {
    mouseinput = Input.mousePosition;
    mouseinput.z = 0; // for no rotation in z direction
    inputRotation = mouseinput - new Vector3(Screen.width * 0.5f, Screen.height * 0.5f,0);
    ProcessMovement();
    }
    void ProcessMovement(){
    transform.rotation = Quaternion.LookRotation(inputRotation);
    }
Aryaman Gupta
  • 616
  • 1
  • 8
  • 20