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
1
Question by Brosiscreations · Feb 06, 2021 at 09:19 PM · touchspeeddragx-axis

Touch movement on drag only with speed

I'm completely lost so I hope someone can help me. I've got a 2D level, I need the player to move left and right only by dragging. How do I do this without teleporting and with a set speed? This is the first time I'm attempting touch controls, I'm still quite new to unity and I'm just not getting it. I've spent the majority of the day looking at questions and watching videos, I'm still no closer to figuring it out. I've tried different things with no luck, I'm finding it hard to understand. I've attached what I have at the moment, this drags left and right but does nothing with speed, if I touch a different part of the screen the player will just teleport there. Any help would be much appreciated. public float moveSpeed;

        void Move()
             {
                 if (Input.touchCount > 0)
                 {
                     if (GameManager.instance.speedUpPressed == true)
                     {
                         Vector3 screenPos = Input.mousePosition;
                         screenPos.z = 10.0f;
                         Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
     
                         Vector3 newPos = transform.position += Vector3.right * moveSpeed * 1.5f * Time.deltaTime;
                         newPos.x = worldPos.x;
                         transform.position = newPos;
                     }
                     else
                     {
                         
                         {
                             //inputX = Input.touchCount;
                             Vector3 screenPos = Input.mousePosition;
                             screenPos.z = 10.0f;
                             Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
     
                             Vector3 newPos = transform.position += Vector3.right * moveSpeed * Time.deltaTime;
                             newPos.x = worldPos.x;
                             transform.position = newPos;
                         }
                     }
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
0

Answer by SadBread · Feb 06, 2021 at 11:19 PM

@Brosiscreations Hi, The bread is here. There are many ways to get the player to move towards the inputted location. The first method is to use the built in unity methods that automatically moves objects to the location. This would include the Vector3.lerp and Vector3.MoveTowards methods.

https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

So, here the code would look like this: Make sure to create a new float or variable that is the speed

1)

 transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);

2)

 transform.position = Vector3.MoveTowards(transform.position, newPos, speed * Time.deltaTime);

However, I am assuming that you only want the player to move left and right by dragging left and right. The way you do that is by getting the distance of the Input.X direction subtracted by the vector of the player. Once you do that, then you can determine how far ahead the input location is in front or behind of you.

Set a float in update to the value of Input.x - Player.position.x, to something for example, MovementDirection.

 MovementDirection = Input.x - Player.position.x;

With that float you have the options to make the movement gradual based off its value or a discrete value:

What I mean:

 rb.velocity += new vector3(MovementDirection * Time.fixedDeltaTime, 0,0);

Or:

     if(MovementDirection > 0){
     rb.velocity += new Vector3(MovementSpeed * Time.fixedDeltaTime, 0,0);
     }
     
     if(MovementDirection < 0){
     rb.velocity += new Vector3(-MovementSpeed * Time.fixedDeltaTime, 0,0);
     }
   

I hope this helps! Good luck programming!

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 Brosiscreations · Feb 12, 2021 at 11:05 PM 0
Share

Let me just start by saying thank you very much for your answer and sorry for the delay. I really do appreciate your in depth response. I tried straight away to try and figure this out with the help of what you have said but was still just not getting anywhere. I think I'm just not understanding this, so I took a break from trying the touch control. Now I'm back trying to figure this out and I'm thinking that I need to learn more to understand this, but you have given me a few things to think about. Now I'm considering something simpler like button controls until I learn more. Thank you

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

140 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

Related Questions

Movement like in Color Road 2 Answers

Starting drag onMouseEnter 0 Answers

How to instantiate object at touch position? 3 Answers

Dragging an object by touch? 6 Answers

How to increase the acceleration of a falling object? 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