Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by IrfanApt · Jan 01, 2014 at 09:15 AM ·

How to Move rigidbody2D on touch ?

Hi all, I have done on touch to move object anywhere on screen. But my requirement is to just touch particular object and move on. Any help please ?? This is my code:

 if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
                         Vector2 touchdeltaposition = Input.GetTouch (0).deltaPosition; 
                         anglex = (float)(touchdeltaposition.x * Time.deltaTime * 10.0); 
                         angley = (float)(touchdeltaposition.y * Time.deltaTime * 10.0);
                         elevationAngle = new Vector3 (anglex, angley, 0);
                         Vector3 elevation = Quaternion.Euler (elevationAngle) * transform.forward;
                         rigidbody2D.AddForce (elevation/2);
                         rigidbody2D.velocity = new Vector2 (anglex, angley);
                 }
Comment
Add comment · Show 1
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 robertbu · Jan 01, 2014 at 09:17 AM 0
Share

I did a quick format of your code, but it still does not look right. You might want to edit your question, paste in a new copy of the code and use the 101/010 button to format the code.

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by MattTheProgrammer · Jan 01, 2014 at 11:14 AM

In my 2d Platformer (Unity 4.3) I had 3 scripts for left & right movement. One was Player.cs (Player Raycasting, Left and right movement, etc), MoveLeft.cs and Move Right.cs.

With Player.cs I had a setup Like this (Attached to Player)

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public float PlayerMoveSpeed = 3f;
     public static bool PlayerMoveRight = false;
     public static bool PlayerMoveLeft = false;
     // Use this for initialization
     void Update()
     {
         //Run these to functions
         Movement();
     }
 
 void Movement()
     {
         //Touch Controls
 
         if(MoveRight == true)
         {
             transform.Translate(Vector2.right * PlayerMoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
             //Left Movement triggered
         if(MoveLeft == true)
         {
             transform.Translate(Vector2.right * PlayerMoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 180);  
         }
         }
    }

The variables MoveLeft & MoveRight would be set to true/false with these scripts..

MoveLeft.cs... For Left movement on touch (Attach to guiTexture Called MoveLeft)

 using UnityEngine;
 using System.Collections;
 
 public class MoveLeft : MonoBehaviour {
     
     // Update is called once per frame
     void Update () 
     {
         //Is there a touch?
         if(Input.touches.Length <= 0)
         {
             //If no touches...
         }
         else //If screen touched..
         {
             //Loop through these
             for(int i = 0; i < Input.touchCount; i++)
             { 
                 //executes this code for current touch
                 if(this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     //If current touch hits the guitexture...
 
 //MoveRight bool within PlayerLogic = false when touch has Began                if(Input.GetTouch(i).phase == TouchPhase.Began)
                     {  
                         Player.PlayerMoveLeft = true;
                     }
 //MoveLeft bool within PlayerLogic = false when touch has ended                    if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                     Player.PlayerMoveLeft = false;
                     }
                 }
             }
         }
     }
 }

MoveRight.cs... For Left movement on touch (Attach to guiTexture Called MoveRight)

 using UnityEngine;
 using System.Collections;
 
 public class MoveRight : MonoBehaviour {
 
     // Update is called once per frame
     void Update () 
     {
         //Is there a touch?
         if(Input.touches.Length <= 0)
         {
             //If no touches...
         }
         else //If screen touched..
         {
             //Loop through these
             for(int i = 0; i < Input.touchCount; i++)
             { 
                 //executes this code for current touch
                 if(this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     //If current touch hits the guitexture...
                     if(Input.GetTouch(i).phase ==  TouchPhase.Began)
                     { 
                         Player.PlayerMoveRight = true;
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                         Player.PlayerMoveRight = false;
                     }
                 }
             }
         }
     }
 }

Change script names if needed and If you're using Javascript Google c# to javascript online and click on the first result.

Comment
Add comment · Show 1 · 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 Rustam-Ganeyev · Apr 28, 2015 at 07:45 PM 0
Share

bad answer since physics does not work when transform is being changed

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

19 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

Related Questions

A node in a childnode? 1 Answer

Help Understanding the Use of Unity with Android 1 Answer

Trying to drag an object with touch using Rigidbody2D.MovePosition 1 Answer

Rotating a spirit, right to left, how to limit rotation angel? 1 Answer

'Use SetTriangles instead. Internally this function will convert the triangle strip to a list of triangles anyway.' (27,74) & (130,73) & (177,30) 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