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