Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera rotation script problem with touch

Discussion in 'Scripting' started by Goldensnitch, Nov 10, 2016.

  1. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Hello. I am having very big issue with this script. Rotation is working fine as i expected but there is one problem that pull my hair off. Whenever i press on screen with my finger obj is reverse back to the same position that i start rotating from. Please help me. Thank you.


    using System;
    using UnityEngine;

    public class touch : MonoBehaviour
    {
    public GameObject Obj = null;
    public float minX = -360.0f;
    public float maxX = 360.0f;

    public float minY = -45.0f;
    public float maxY = 45.0f;

    public float sensX = 100.0f;
    public float sensY = 100.0f;

    float rotationY = 0.0f;
    float rotationX = 0.0f;

    float MouseX;
    float MouseY;

    void Update()
    {
    var x = Input.GetAxis("Mouse X");
    var y = Input.GetAxis("Mouse Y");
    if (x != MouseX || y != MouseY)
    {
    rotationX += x * sensX * Time.deltaTime;
    rotationY += y * sensY * Time.deltaTime;
    rotationY = Mathf.Clamp(rotationY, minY, maxY);
    MouseX = x;
    MouseY = y;
    Obj.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    }
    }
    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Please use code tags to post your code.

    The problem is that on a touch device, the mouse position reported warps instantly from wherever the last touch was, to where the finger goes down now. That gives you a huge MouseX (delta) that your code applies to undo most or all of the rotation it did before (and same for MouseY).

    To fix this, keep track of whether the mouse was down on the last frame, just like you keep track of the previous MouseX and MouseY. Now, when the mouse goes down (i.e. it's down now and wasn't before), set MouseX and MouseY to your the current mouse position right away.
     
  3. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60

    How do i do that i am new i found this script from some website. I am very bad at codding can you help me please
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Sorry, I'm about to dash off for a 7-hour car trip... and the wifi in the car is terrible. @BoredMormon, perhaps you have time to show him the path?

    @Goldensnitch, I will say that you won't get very far with Unity without being able to do at least basic coding. Spend the time going through the tutorials in the Learn section (see the link at the top of this page). Also work through learncs.org. These skills are valuable (and, you may discover, a lot of fun too!).
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You want to start from the beginning. What behaviour are you trying to create? How should the touch position relate to the objects rotation. What should happen when the touch is moved or released?

    It's not immediately obvious what you are trying to do here.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I think he just wants it so that, when he drags his finger to the left, the object rotates to the left, and then stays there after the touch is released (and can be rotated more by another drag).

    (I know, I know, I'm on my way out the door now...)
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Simply wrapping it in an if statement checking the touch count might work.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Try this. It's actually simpler than I was thinking, and more like what @BoredMormon said: you just need to skip doing any updating on the frame when the mouse button (touch) first goes down. I also removed some of the extra stuff that had no business being there, just to simplify things a bit.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class touch : MonoBehaviour
    5. {
    6.     public GameObject Obj = null;
    7.  
    8.     public float minY = -45.0f;
    9.     public float maxY = 45.0f;
    10.  
    11.     public float sensX = 100.0f;
    12.     public float sensY = 100.0f;
    13.  
    14.     float rotationY = 0.0f;
    15.     float rotationX = 0.0f;
    16.  
    17.     void Update() {
    18.         if (!Input.GetMouseButtonDown(0)) {
    19.             var x = Input.GetAxis("Mouse X");
    20.             var y = Input.GetAxis("Mouse Y");
    21.             rotationX += x * sensX;
    22.             rotationY += y * sensY;
    23.             rotationY = Mathf.Clamp(rotationY, minY, maxY);
    24.             Obj.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    25.         }
    26.     }
    27. }
     
  9. Deathcrew

    Deathcrew

    Joined:
    Nov 12, 2016
    Posts:
    7
    thanks... it help me on the tutorial for beginnig because im new ^_^
     
  10. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    THank you JOESTROUT
     
  11. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    This Script works well. Thank you so much. Do you know how to zoom into selected object. Can you help me with this one.
     
  12. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Also thank to you my friend. I am working on human skeleton anatomy application. And stuck with scripts :)
     
  13. Deathcrew

    Deathcrew

    Joined:
    Nov 12, 2016
    Posts:
    7

    I think that work... But when i try on my device... Work only TAPTOMOVE script... And the camera rotation not work... Where is my error? Actually I insert script rotation on object that I want to move... I try to insert on camera? Thanks for help