1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

rotate towards touch position around Z

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

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Messages:
    1,457
    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
    Messages:
    1,457
    come on it can't be that hard for one off you guys to look at this and see whats wrong... ;-)