Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

rotate towards touch position around Z

Discussion in 'Android' started by jister, Apr 29, 2012.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey, i'm making my first android game and got a bit stuck at what i tought was going to be fairly simple.

    i got a game object that always moves forward and i want it to rotate smoothly towards the touch position so that it makes a nice curve.

    I'm only coding for a good 6 months learning from a friend at work bit by bit so i still get a bit confussed with all this quaternion stuff.
    anyway this is what i got so far, it does sometimes what i want :)

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Controller : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public Vector3 axis;
    8.    
    9.    
    10.     void Update () {
    11.        
    12.           int i = 0;
    13.         while (i < Input.touchCount ) {
    14.            
    15.             if (Input.GetTouch(i).phase == TouchPhase.Began){
    16.                
    17.                 Vector3 touchPosition = Input.GetTouch(0).position;
    18.                 Vector3 relativeTP = touchPosition - transform.position;
    19.                 Quaternion targetRotation =  Quaternion.Euler (0,0,relativeTP.x + relativeTP.y );
    20.                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
    21.  
    22.             }
    23.  
    24.            
    25.             ++i;
    26.         }
    27.     }
    28.  
    29.     void FixedUpdate () {
    30.      float autospeed = 5f;
    31.         transform.Translate(axis * (Time.deltaTime * autospeed));
    32.        
    33.     }
    34. }
     
    Last edited: Apr 29, 2012
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    come on it can't be that hard for one off you guys to look at this and see whats wrong... ;-)