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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Euler angles not work right

Discussion in 'Scripting' started by kondor, Oct 17, 2016.

  1. kondor

    kondor

    Joined:
    Jun 18, 2013
    Posts:
    48
    Hello everyone,
    so O wrote this code:
    public class playerControl : MonoBehaviour
    {
    public float rotationSpeed;
    public float xMin;
    public float xMax;
    public float yMin;
    public float yMax;

    private float x, y;
    private Vector2 toRoate;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
    for(int i = 0;i < Input.touchCount;i++)
    {
    if(Input.GetTouch(i).phase == TouchPhase.Moved)
    {
    x = Input.GetTouch(i).deltaPosition.x;
    y = Input.GetTouch(i).deltaPosition.y;
    toRoate = new Vector2(y * rotationSpeed,-x * rotationSpeed);
    transform.eulerAngles = toRoate;
    }
    }
    }
    }

    and the problem is that the object rotate's like its stuck,In the start I used transform.Rotate but it resulted effect on the Z axis so I changed it to eulerangles and now it work just on the x and the y as I wanted but now its move like it's stuck and I need to drag my finger in force to make it move
    how can I fix the problem?
    thanks for the help
     
  2. otz20100

    otz20100

    Joined:
    Oct 17, 2016
    Posts:
    13
    I think Input.GetTouch(i).deltaPosition.x; is the problem...

    Delta position : The position delta since last change.. That's why you need to drag your finger.

    Try to use Input.GetTouch(i).position

    https://docs.unity3d.com/ScriptReference/Touch.html

    Hope it helps.
     
  3. kondor

    kondor

    Joined:
    Jun 18, 2013
    Posts:
    48
    HI thanks for the help its fixed the problem...kind of no the problem is that when I touch the screen it take new rotation
    I need to find a way to add the new rotation to the current one so it won't jump when I stop touch the screen and then touch again
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You seem to be just setting the rotation, rather than adding to it.. change this line:
    Code (CSharp):
    1. transform.eulerAngles = toRoate;
    To:
    Code (CSharp):
    1. transform.eulerAngles = transform.eulerAngles + toRoate;
    Note you'll probably have to change toRoate to a Vector3
    Code (CSharp):
    1. toRoate = new Vector3(y * rotationSpeed,-x * rotationSpeed);