Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by typane · Jun 15, 2012 at 09:57 AM · rotationmobilepivot

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");
         }
        }    
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by typane · Jun 16, 2012 at 07:45 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by typane · Jun 16, 2012 at 07:35 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotation of Object on single axis in direction of the mouse position 0 Answers

Rotate Vector3 array around a point 1 Answer

Accelerator.x rotating camera.z - problems with intermediate values 0 Answers

Animation rotation problem. 0 Answers

Rotating around a pivot point using a Quaternion? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges