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 leviwickss · May 24, 2019 at 05:23 AM · mobiletouchquaternionaddforcetouchcount

How to make function respond to only the initial touch position.

In this game what I'm trying to accomplish is a sprite that rotates towards the touch position (which I have done) and have knock-back. The problem i have run into now is that it rotates if you hold your finger down on your screen. I'm trying to make it so that it only rotates and applies force to the initial touch position, and if they want to change that position for the user to take off his finger and touch somewhere else.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TouchMove : MonoBehaviour
 {
     public float speed = 100;
     public Transform ttouch;
     public Rigidbody2D myRigidbody;
     public float knock = 40;
 
     // Start is called before the first frame update
     void Start()
     {
         knock = knock * -1;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
             ttouch.position = touchPosition;
             Rotate();
             Knockback();
         }
 
     }
 
 
     void Rotate() {
         //Rotate towards touch
         Vector3 direction = ttouch.position - transform.position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
     }
 
     void Knockback() {
         //Knock back applied when screen touched
         myRigidbody.AddForce(transform.right * knock);
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by mchts · May 24, 2019 at 06:44 AM

I edited my answer as your needs. You should store touch position on initial touch. So your code should be like:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class TouchMove : MonoBehaviour
  {
      public float speed = 100;
      //public Transform ttouch;
      public Rigidbody2D myRigidbody;
      public float knock = 40;
      
      Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
  
      // Start is called before the first frame update
      void Start()
      {
          knock = knock * -1;
      }
  
      // Update is called once per frame
      void Update()
      {
          Touch touch = Input.GetTouch(0);
          if(touch.phase == TouchPhase.Began){
                touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
                //ttouch.position = touchPosition;
          }
          Rotate();
          Knockback();
      }
  
  
      void Rotate() {
          //Rotate towards touch
          Vector3 direction = touchPosition - transform.position;
          float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
          Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
          transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
      }
  
      void Knockback() {
          //Knock back applied when screen touched
          myRigidbody.AddForce(transform.right * knock);
      }
  } 

Comment
Add comment · Show 3 · Share
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
avatar image leviwickss · May 24, 2019 at 03:10 PM 0
Share

Thank You! This particular script did not completely work, but you showed me the Touch.phase which eventually solved it. I did happen to run into a couple errors though. One problem that came up was me not being able to access my touchPosition from my rotate function, which I ended up having to get rid of all together so it looks a little messy. Also the second is an Error which says: Argument Exception: Index out of Bounds. Seems like it has something to do with my touch position on screen or maybe the unity remote app.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Touch$$anonymous$$ove : $$anonymous$$onoBehaviour
 {
     public float speed = 100;
     public Transform ttouch;
     public Rigidbody2D myRigidbody;
     public float knock = 40;
 
     // Start is called before the first frame update
     void Start()
     {
         knock = knock * -1;
     }
 
     // Update is called once per frame
     void Update()
     {
         Touch touch = Input.GetTouch(0);
         Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);

         switch (touch.phase)
         {
             case TouchPhase.Began:
                 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
                 //ttouch.position = touchPosition;
                 Vector3 direction = touchPosition - transform.position;
                 float angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
                 Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
                 $$anonymous$$nockback();
                 break;
         }
     }
 
     void $$anonymous$$nockback()
     {
         //$$anonymous$$nock-back applied when screen touched
         myRigidbody.AddForce(transform.right * knock);
     }
 }
avatar image mchts leviwickss · May 24, 2019 at 03:27 PM 1
Share

A simple check will do this:

 void Update() {
     if(Input.touchCount > 0) //this line here will make you avoid index out of bounds errors
     {
         Touch touch = Input.GetTouch(0);
         Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
 
          switch (touch.phase)
          {
              case TouchPhase.Began:
                  touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
                  //ttouch.position = touchPosition;
                  Vector3 direction = touchPosition - transform.position;
                  float angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
                  Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
                  transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
                  $$anonymous$$nockback();
                  break;
          }
      }
 }




avatar image leviwickss mchts · May 24, 2019 at 03:28 PM 0
Share

Thank you, this works perfectly

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

184 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.touchCount returning wrong number 0 Answers

Fix Rotate Object with touchscreen on X and Y axis 0 Answers

How to touch, move and rotate 3D objects in XZ? 1 Answer

,How do i make a D-Pad that functions on AddForce?? 0 Answers

Changing keyboard input to touch in script 0 Answers


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