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 Schmeep · Sep 11, 2018 at 08:04 PM · androidnewbietouch controlsnewnew user

No coding experience, tried to mash two codes together, but they aren't playing nicely

So the idea is to get the cube i'm currently using to move 1.25 right and -1.25 left which I fixed in its code, and the other one uses touch in order to move the character, but after trying to export an .apk it doesn't seem to work. Would really appreciate some help! Attached is the whole code I'm using:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     //variables
     public GameObject character;
     private float speed = 5.0f;
     private Vector3 pos;
     private Transform tr;
     private Rigidbody2D characterBody;
     private float ScreenWidth;
 
 
     // Use this for initialization
     void Start()
     {
         pos = transform.position;
         tr = transform;
         ScreenWidth = Screen.width;
         characterBody = character.GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         int i = 0;
         //loop over every touch found
         while (i < Input.touchCount)
         {
             if (Input.GetTouch(i).position.x > ScreenWidth / 2 && tr.position == pos)
             {
                 //move right
                 pos += new Vector3(1.25f, 0, 0);
             }
             if (Input.GetTouch(i).position.x < ScreenWidth / 2 && tr.position == pos)
             {
                 //move left
                 pos += new Vector3(-1.25f, 0, 0);
             }
             ++i;
         }
     }
 }

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 DevsGoingViral · Sep 15, 2018 at 06:35 AM 0
Share

Problem is as soon as tr.position is not equal to pos your code will stop working. Basically your code will start and check is current position is same as starting position,once you tap that would be true for a split seconds until code hit next line : pos+= new Vector3(1.25f,0,0),right that moment tr.position is not equal to pos so your pos+= new Vector3(1.25f,0,0) will not work,that code will not be executed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Sep 15, 2018 at 12:36 PM

Good day.

You say when yo uexport does not work, but, it works in the editor?

I did not read all the code, dont know what you want to get, and never did a project for mobile, so Input.touch family functions are a mistery for me. But, if you read the manual will see some incongruences

 [....]
 void Update()
      {
          int i = 0;
          //loop over every touch found
          while (i < Input.touchCount)
  [....]

If you read the Input.touchCount manual the only it says is:

Number of touches. Guaranteed not to change throughout the frame. (Read Only)

So, if Update is called only once per frame, the value of touchCount will be always the same during that frame.

It can be this? Did you debuged the code to see how the number increases?

Bye!

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

209 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Issues with Translations and OnTriggerExit 0 Answers

this works for moving, but after i move either left or right it starts slowly moving me in the opposite direction, why? 0 Answers

Help with Putting GameObjects into a list. 2 Answers

How do I check if a spot in a cell is missing? 2 Answers

What type of code should I learn for a new game? 3 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