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 David92 · Jan 18, 2017 at 03:41 PM · c#movement3dtouch

How to move character horizontally with Touch, while player always runs forward?

Hey guys,

So what I am doing is an Android Game, where I have a player, who runs forward since the start of game. I do this with following code:

  void Update ()  {
              // Assign player to always go forward, continously 
          transform.position += transform.forward * Time.deltaTime * speed;
 
         }

Now I would like to receive Touch Inputs from Android Devices and I only care (for now) about x direction (so left and right, based on where on the screen was touched).

I have found several answers here on Unity3D, but each of those I tried, did not have the effect I want to achieve (= so to continue running forward, but just moving the position to either left or right).

User should hold the finger to continue moving left or right... touching the screen only, should just move the player for a slight distance.

Any advice?

Thanks

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

2 Replies

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

Answer by Hellium · Jan 18, 2017 at 03:42 PM

You can try something like this :

         Vector3 deltaPosition = transform.forward * forwardSpeed;

         if( Input.touchCount > 0 )
         {
             Vector3 touchPosition = Input.GetTouch( 0 ).position;
             if ( touchPosition.x > Screen.width * 0.5f )
                 deltaPosition += transform.right * sideSpeed;
             else
                 deltaPosition -= transform.right * sideSpeed;
         }

         transform.position += deltaPosition * Time.deltaTime ;
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 David92 · Jan 18, 2017 at 03:49 PM 0
Share

Better than others I have tried, but I still have a bit strange issue, because when I touch right-side of screen, it drags character somehow forward-right, not just right. And if I do left-touch, it goes backwards-left. Does the sideSpeed need to be equal forwardSpeed?

avatar image Hellium David92 · Jan 18, 2017 at 04:24 PM 0
Share

I edited my answer, I made a huge mistake when setting only the x coordinate of the delta vector.

avatar image David92 Hellium · Jan 18, 2017 at 04:28 PM 0
Share

Works now, thanks Hellium!

avatar image
0

Answer by nathi0106 · Feb 14, 2019 at 06:50 PM

Hi. I'm also trying to android-ify my horizontal-movement-game... But can't.. (I'm a noob in unity and scripting btw). Since, this script is working, I would like to ask, do i have to define forward speed and also side speed as 2 different variables?? I desperately need help on this. I have attached the script i used to move my character using keyboard. Please help. Thanks in advance.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BasicMovement : MonoBehaviour {
 
     public Animator animator;
 
     public float speed;
 
     private void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update () {
 
         animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
 
         Vector3 horizontal = new Vector3(Input.GetAxis("Horizontal"), 0.0f, 0.0f);
         transform.position = transform.position + horizontal * speed * Time.deltaTime;
 
 
     }
 }
Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Separate Objects after Mouse Clicking in Unity 5.6.4 1 Answer

Problem with movement 0 Answers

Player not following touch after camera is rotated? 0 Answers

Multiple Cars 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