x


Mobile rotation on pivot

I am currently trying to rotate an object around its pivot point. It's pivot point is placed at the back left of the image (note: working with 2D planes). This works fine and all but I am intending to mathf.Clamp the axis between 0 and 90. The issue I am having is that when you approach 90 degree whilst moving your finger it kind of dies off at 80 degrees so its not a crisp movement. If you take your finger off and move it, it works fine but of course you want the ability of one smooth up and down motion without having to remove and move the object again. It has to do with the rotation, I can't think of another way that would do it, I worked with Euler and Quaternion but it seems if you extend your finger to the outset of the barrel and move it, it works better. Essentially the only other solution I can think of is creating an invisible plane at which the cannon barrel looks at (snaps to) and follows on wards but this seems rather unnecessary.

Code below, its all very hard coded for now till I can resolve this issue.

using UnityEngine;
using System.Collections;

public class CannonBarrel : MonoBehaviour {

private bool isCannonTouched;

private float speed;
private float minClamp;
private float maxClamp;

void Start() {
    speed = 0.4f;
    isCannonTouched = false;
    maxClamp = 90;
    minClamp = 0;
}

void Update(){
    TapSelect ();
}

void TapSelect()
{   
foreach (Touch touch in Input.touches) {
    if (touch.phase == TouchPhase.Began) {
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit ;
        if (Physics.Raycast (ray, out hit)) {
             //hit.transform.SendMessage("Selected");
         //print("hit an object");
         if (hit.collider.gameObject.name == this.gameObject.name) {//barrel(underscore)standin
          print ("isCannonTouched is now true");
          isCannonTouched = true;
          }
         //print ("Look ma no hands");
         //hit.collider.gameObject.transform.rotation = Quaternion.Euler(200,-30,50);

            }
        }
       if (Input.GetTouch(0).phase == TouchPhase.Moved && isCannonTouched)
       {
          Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
          print ("Currently moving the cannon");
          //Vector3 gameStore = this.gameObject.transform.rotation.eulerAngles;
              // gameStore = new Vector3(
          //transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 
          //Mathf.Clamp (transform.rotation.eulerAngles.z, 0, 90));
          //TODO issue at this point
          this.gameObject.transform.Rotate (0,0,  touchDeltaPosition.x * speed);

       }
       if (Input.GetTouch(0).phase == TouchPhase.Ended)
       {
          isCannonTouched = false;
          print("the touch phase has ended and isCannonTouched is now false");
       }

} } }

more ▼

asked Jun 15 '12 at 09:57 AM

typane gravatar image

typane
3 2 5 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I have changed the way I am going about this using the lookAt function.

transform.LookAt(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(Camera.main.transform.position.z))));

I still can't seem to get this working, it just rotates in random axis. I've tried locking the axis but I don't know whats going on.

more ▼

answered Jun 16 '12 at 07:45 AM

typane gravatar image

typane
3 2 5 8

(comments are locked)
10|3000 characters needed characters left

After a lot of searching an multiple trials I have a solution if anyone was curious. Essentially this allows the object which is a 2D plane to follow mouse and finger input.

//NewRotationWithMouse() public Vector3 mouse_pos; public Transform target; public Vector3 object_pos; public float angle;

//NewRotationWithFinger()
public Vector3 finger_pos;

target = this.transform

void Update() { NewRotationWithFinger(); //NewRotationWithMouse(); }

void NewRotationWithFinger()
{
    if (Input.touchCount > 0)
    {
       foreach (Touch touch in Input.touches)
       {
         if (Input.GetTouch(0).phase == TouchPhase.Moved)
         {
         finger_pos = Input.GetTouch(0).position;
         finger_pos.z = 5.23f;
         object_pos = Camera.main.WorldToScreenPoint(target.position);
         finger_pos.x = finger_pos.x - object_pos.x;
         finger_pos.y = finger_pos.y - object_pos.y;
         angle = Mathf.Atan2 (finger_pos.y, finger_pos.x) *Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0,0,angle));
         this.gameObject.transform.rotation = transform.rotation;
         }
       }
    }
}

void NewRotationWithMouse()
{
       mouse_pos = Input.mousePosition;

mouse_pos.z = 5.23f; //The distance between the camera and object object_pos = Camera.main.WorldToScreenPoint(target.position); mouse_pos.x = mouse_pos.x - object_pos.x; mouse_pos.y = mouse_pos.y - object_pos.y; angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); }

If this doesn't work feel free to contact me.

more ▼

answered Jun 16 '12 at 07:35 AM

typane gravatar image

typane
3 2 5 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2154
x410
x84

asked: Jun 15 '12 at 09:57 AM

Seen: 565 times

Last Updated: Jun 16 '12 at 07:45 AM