- Home /
Rotate GameObject based on Touch Input Vector (2D)
I know, yet another rotation question. I have been reading many many posts, but am still struggling on getting my game code right. Rotation is just my main priority at the moment, but if anyone has suggestions otherwise I am up for anything. I am new to Unity, so there may be "best practices" that I am not following. Here is my situation:
2D game controlling a single sprite/gameobject with touch input (imagine for simplicity just a 2D head looking right or left - circle collider to allow smooth rolling in some situations)
Swiping (touch screen swipe) will cause the sprite to "jump" (Add force in direction of the swipe)
The gameobject should 'headbutt' in the direction of the swipe - rotate the correct amount based on the vector of the input swipe to face that vector
Would like the head to try and land on the ground so that it is 'upright' - another rotation before hitting ground or immediately after
I will need to write code to detect if a collision with an enemy is a headbutt of not (damage the enemy or not), so any suggestions
For the rotation code I have tested with Quaternion.slerp, also with Quaternion.LookRotation, but either my code doesn't compile or does nothing. These require 3D vectors, and I am creating a 2D vector with the input - which is just one of my points of struggle. This is just for the rotation to look towards the touch input vector, I haven't even started rotation code to land the head correctly, but am thinking that I may have a ray looking down for the ground and if it collides it will begin a rotation.
Any suggestions?
Sample Code:
public class HeadSwipeMovement : MonoBehaviour {
public float minSwipeDistY;
private Vector2 startPos;
public GameObject head;
public Rigidbody2D rb;
public CircleCollider2D pc;
public int jumpCount;
void Start () {
minSwipeDistY = 0; //Will set a min 'height' for jump later
head = GameObject.FindGameObjectWithTag ("Head");
rb = head.GetComponent<Rigidbody2D> ();
pc = head.GetComponent<CircleCollider2D> ();
jumpCount = 0;
}
void FixedUpdate () {
if (Input.touchCount == 1) {
Touch touch = Input.touches[0];
switch (touch.phase){
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
float swipeDistY = (new Vector2(0, touch.position.y) - new Vector2(0, startPos.y)).magnitude;
// Jump counts less or equal to 0 needed to keep at 2 jumps - I do not know why...yet
if ((swipeDistY > minSwipeDistY) & (jumpCount <= 0)){
float SwipeValueY = touch.position.y - startPos.y;
float SwipeValueX = touch.position.x - startPos.x;
//Increment jumpCount to limit # jumps
jumpCount++;
//Add force to head
rb.AddForce(new Vector2 (SwipeValueX, SwipeValueY));
}
break;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612002703im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D character rotation wrong direction when moving 1 Answer
Rotating an object towards target on a single axis 2 Answers
Rigidbody2d Collision Breaks Quaternion.Slerp 2 Answers
My player is not Rotating as should 0 Answers
Homing missile not working. 1 Answer