1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Reddit AMA with Adam Myhill on Cinemachine, Thurs July 27 - 10AM PT. More info.
    Dismiss Notice
  6. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  7. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    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,560
    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,560
    come on it can't be that hard for one off you guys to look at this and see whats wrong... ;-)