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

help with object move with rotation by touch

Discussion in 'Scripting' started by Ammark93, Oct 22, 2015.

  1. Ammark93

    Ammark93

    Joined:
    Oct 22, 2015
    Posts:
    5
    hello. iam kind new here ^^
    iam working on new unity project which i want to move my player object right and left based on two gui textures on right and left of the screen,
    when press right and left it goes to rotation z like 5 and -5. without smoothly rotate.

    I'ts moving right and left, but it's not smoothly rotation when moving..
    how to fix the rotation and make it smooth?
    sorry for my bad English ^^

    so this is my script without gui textures..

    Code (CSharp):
    1. void FixedUpdate ()
    2.  {
    3.  
    4. float LeftRight = 0;
    5.  
    6. if(Input.touchCount > 0){
    7.  
    8. // touch x position is bigger than half of the screen, moving right
    9. if(Input.GetTouch(0).position.x > Screen.width / 2)
    10. LeftRight = 1f;
    11.            
    12. // touch x position is smaller than half of the screen, moving left
    13. else if(Input.GetTouch(0).position.x < Screen.width / 2)
    14. LeftRight = -1f;
    15.  }
    16.  
    17. Vector3 movement = new Vector3 (LeftRight, 0.0f, 0.0f);
    18.      
    19.  
    20. Rigidbody.velocity = movement * speed;
    21.      
    22. Rigidbody.position = new Vector3   (Mathf.Clamp(Rigidbody.position.x, xMin, xMax),0.0f,
    23. Mathf.Clamp(Rigidbody.position.z, zMin, zMax));
    24.      
    25.  Rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, Rigidbody.velocity.x * -tilt);
    26.        }


    View attachment 159177
     
  2. Ammark93

    Ammark93

    Joined:
    Oct 22, 2015
    Posts:
    5
    sorry i posted two threads..
     
    Last edited: Oct 22, 2015
  3. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    The way I do touch is buttons its simple,

    Code (csharp):
    1.  
    2. public gameobject Object;
    3. void buttonturned(){
    4.  
    5. transform.translation(Object.Vector3, left) * time.deltatime * 1;
    6.  
    7. }
    8.  
    9.  
     
  4. Ammark93

    Ammark93

    Joined:
    Oct 22, 2015
    Posts:
    5
    hey, thank you
    but it didn't helped me, my controls is working in my script, What i want to fix is my rotation because its not smoothly rotate. when i move right for example, the number of "Z" rotation changed from 0 to 5 .. which makes it not smooth..
    btw, is your code is like this?

    Code (csharp):
    1.  
    2. public Gameobject Object;
    3. void buttonturned(){
    4. Object.transform.Translate(Vector3.left * time.deltatime * 1);
    5. }
    6.  
     
    Last edited: Oct 22, 2015
  5. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    I think I didn't code it right but you can look it up. Also, it dose that because the origin in off center. its 99.9% impossible to get the origin in the exact middle. I'm guessing its in a parent so, look at parent origin. Move the objects in the parent closer to the origin. Closer the better. :D
     
  6. Ammark93

    Ammark93

    Joined:
    Oct 22, 2015
    Posts:
    5
    Thank you ! But I've managed to fix it ^^
    Here is my final code script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5.  
    6.  
    7.  
    8. public class PlayerMovementLeft : MonoBehaviour {
    9.  
    10.  
    11.     private float speed = 20f;
    12.     public float xMin, xMax, zMin, zMax;
    13.     public float LeftRight;
    14.  
    15.  
    16.  
    17.     void FixedUpdate ()
    18.     {
    19.         float LeftRight = 0;
    20.         if(Input.touchCount > 0)
    21.         {
    22.             // touch x position is bigger than half of the screen, moving right
    23.             if(Input.GetTouch(0).position.x > Screen.width / 2)
    24.             {
    25.                 rotateRight();
    26.                 LeftRight = 1f;
    27.             }
    28.  
    29.             // touch x position is smaller than half of the screen, moving left
    30.             else if(Input.GetTouch(0).position.x < Screen.width / 2)
    31.             {
    32.                 rotateLeft();
    33.                 LeftRight = -1f;
    34.             }
    35.         }
    36.  
    37.         rotateOrignel ();
    38.         Vector3 movement = new Vector3 (LeftRight, 0.0f, 0.0f);
    39.         GetComponent<Rigidbody>().velocity = movement * speed;
    40.  
    41.         GetComponent<Rigidbody>().position = new Vector3(Mathf.Clamp (GetComponent<Rigidbody>().position.x, xMin, xMax), 0.0f, Mathf.Clamp (GetComponent<Rigidbody>().position.z, zMin, zMax));
    42.  
    43.     }
    44.  
    45.  
    46.     void rotateRight()
    47.     {
    48.         Quaternion newRotation = Quaternion.AngleAxis (90, Vector3.back);
    49.         transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, 0.03f * Time.timeScale);
    50.     }
    51.  
    52.     void rotateLeft()
    53.     {
    54.         Quaternion newRotation = Quaternion.AngleAxis (90, Vector3.forward);
    55.         transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, 0.03f * Time.timeScale);
    56.     }
    57.  
    58.     void rotateOrignel()
    59.     {
    60.         Quaternion newRotation = Quaternion.AngleAxis (0, Vector3.zero);
    61.         transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, 0.3f * Time.timeScale );
    62.     }
    63.  
    64. }
    65.