- Home /
How to rotate snake at an exact 90 degrees
I am making a snake game. I want to rotate my snake exact 90 degrees in the direction specified by swiping on the screen.(It's an android game). In this I gained a bit success, can someone provide me with the mathematical formula to rotate snake by determining it's current head direction. As it rotates at 90 degrees correctly but not always in the direction specified by swipe
You will have to do some sort of swipe gesture detection so once an "upwards" swipe is detected that it should move up ins$$anonymous$$d of wherever it was currently heading and likewise for all other directions. You will have to look into Input Touches to do this. It's not an easy task but not impossible. There are many assets on the Asset Store that do touch gesture recognition for this reason.
Since your snake is just spheres you can just change the movement direction ins$$anonymous$$d of doing a real rotation.
Depending on how you want the snake movement it would change how to implement it as well. Do you want the body parts behind the head to move towards the head all the time (even when it changes direction), or do you want the body parts to take the exact full path that the head did?
Leuthil well thanks for the rapid reply. I had enabled the swipe gestures, and you can see snake moves pretty fine. but the problem is that. when i swipe up and snake's head direction is on right-it moves towards down ins$$anonymous$$d of upward :(
You will have to post the code you are using to change the direction of the snake otherwise there's really no way anyone can help :).
$$anonymous$$y Update method snippet : kindly help me please
void Update()
{
transform.Translate(new Vector3(0, 0, 0.09f));
foreach (Touch T in Input.touches)
{
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1)
{
SwipeID = T.fingerId;
StartPos = P;
}
else if (T.fingerId == SwipeID)
{
Vector2 delta = P - StartPos;
if (T.phase == TouchPhase.$$anonymous$$oved && delta.magnitude > $$anonymous$$$$anonymous$$ovement)
{
SwipeID = -1;
if ($$anonymous$$athf.Abs(delta.x) > $$anonymous$$athf.Abs(delta.y))
{
if (delta.x > 0)
{
transform.Rotate(new Vector3(0, 90, 0));
// Debug.Log("Swipe Right Found");
}
else
{
transform.Rotate(new Vector3(0, -90, 0));
// Debug.Log("Swipe Left Found");
}
}
else
{
if (delta.y > 0)
{
transform.Rotate(new Vector3(0, 90, 0));
//Debug.Log("Swipe Up Found");
}
else
{
transform.Rotate(new Vector3(0, -90, 0));
//Debug.Log("Swipe Down Found");
}
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
if (Time.time % 10 < 0.05)
placeFood();
}
}
Answer by Leuthil · Mar 27, 2014 at 02:37 PM
You cannot use transform.Rotate because that will always rotate relative to whichever direction is currently facing. What you want to use instead is Transform.eulerAngles which will explicitly set a rotation despite whatever the current rotation is:
transform.eulerAngles = new Vector3(0f, yAngleRotation, 0f);
Each swipe direction would have a different yAngleRotation value (I'm not sure exactly which as it depends on what axis and direction your game is on / displayed at). These values would probably each be 0f, 90f, 180f, 270f.
One key thing to note is to make sure you don't just set one axis rotation by itself or you will get bad results... as in don't do this:
// this is very very bad, don't do this!!!
transform.eulerAngles.y = yAngleRotation;
Your answer