Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    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,582
    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,582
    come on it can't be that hard for one off you guys to look at this and see whats wrong... ;-)