Question by
umar2121 · Oct 09, 2015 at 04:31 PM ·
rotationdraggingswappingangle adjust
rotate object on touch on just specific angles and make a layer like in snooker
How to rotate the arrow on specific angle like increment on 45 and then 90,135,180 and so on degrees if user press at the right side so it increase and vice versa. Plus need a beem ray exactly like in snooker game ..New in Scripting so need help (C-sharp code)
private float dist; private bool dragging = false; private Vector3 offset; private Transform toDrag; int i;
void Update() {
Vector3 v3;
i = 0;
if (Input.touchCount != 1) {
dragging = false;
return;
}
Touch touch = Input.touches[i];
Vector3 pos = touch.position;
if(touch.phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
{
toDrag = hit.transform;
dist = hit.transform.RotateAround(touch.position,transform.eulerAngles.y,15f) - Camera.main.transform.eulerAngles.y;
Debug.Log (dist);
v3 = new Vector3(pos.x, dist, pos.z);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.eulerAngles - v3;
Debug.Log(offset);
dragging = true;
}
}
if (dragging && touch.phase == TouchPhase.Moved) {
v3 = new Vector3(Input.mousePosition.x,dist , Input.mousePosition.z);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.eulerAngles = v3 + offset;
}
if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
dragging = false;
i++;
}
}
// transform.eulerAngles=new Vector3(transform.eulerAngles.x,angle,0);
}
arrow-up.png
(10.2 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612040700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to rotate my character to follow the terrain 0 Answers
Math: How to rotate a wheel by drag&drop (Canvas, 2D)? 0 Answers
following and rotation help 0 Answers
World Rotation Around The Player Lead me to a Strange Result! 0 Answers
Access 2DCollision Torque/rotation force and set it to 0 0 Answers