Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tollivtl · Sep 05, 2015 at 03:18 PM · 2drotationquaternion

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










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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

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


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