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
0
Question by teking123 · May 15, 2014 at 07:38 AM · touchphonetouch screen

how i can convert thjs code to touch (Input.anyKeyDown)

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 // Use this for initialization
 void Start () {
     isStartButtonPressed = false;
     Time.timeScale = 0.0f;
 }
 
 // Update is called once per frame
 void Update () {
     updateScore();
     if(!isInView())
     {
         restartGame();
     }
     if(Input.anyKeyDown)
     {
         move();
     }
 }
 
 private bool isInView()
 {
     Vector3 port = Camera.main.WorldToViewportPoint(transform.position);
     if((port.x < 1) && (port.x > 0) && (port.y < 1) && (port.y > 0) && port.z > 0)
     {
         return true;
     }
     else
     {
         return false;
     }
     
 }

 private bool isStartButtonPressed;
 public GUIText scoreLabel;
 void OnGUI()
 {
     if (!isStartButtonPressed)
     {
         GUI.TextField(new Rect(Screen.width/2-65, Screen.height/2-11 ,130,22), "Do something to start");
         if(Input.anyKeyDown)
         {
             Time.timeScale = 1.0f;
             isStartButtonPressed = true;
         }
     }
 }

 private void move()
 {
     rigidbody.velocity = new Vector3(0,0,0);
     rigidbody.AddForce (new Vector3(275,200,0), ForceMode.Force);
 }

 void OnTriggerEnter(Collider other)
 {
     restartGame();
 }
 
 private void restartGame()
 {
     Time.timeScale = 0.0f;
     isStartButtonPressed = false;
     Application.LoadLevel (Application.loadedLevelName);    
 }

 private void updateScore()
 {
     int score = (int) (transform.position.x / GenerateWorld.distanceBetweenObjects);
     if(score != (int.Parse(scoreLabel.text)) && score > 0)
     {
         scoreLabel.text = score.ToString();
     }
     
 }

}

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 darthtelle · May 15, 2014 at 09:52 AM 0
Share

That is a lot of code to go through. Is there an issue with all of this code? Could you highlight where in the code the issue is? What is the precise issue you are seeking help with?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · May 15, 2014 at 01:31 PM

Your input is very simple here. You need to make the "Do something to start" go away, and you need to call move() when a single event occurs. You can do the first by checking Input.toucCount. If it is greater than 0, then make the the text go away. Change line 41 from:

 if(Input.anyKeyDown)

to:

 if(Input.touchCount > 0)


For the move(), you likely want to detect a finger down event. You can do that by checking the touch inputs and look for the begin phase. Take a look at the second example script on this page here:

https://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html

Your code, instead of calling Instantiate() would call move().

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

Answer by hari ram · May 15, 2014 at 02:04 PM

Your almost there & need to change just a few things.

Check-out a similar answer posted by me in the following page....

Work with touch inputs now !!!

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

22 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

Related Questions

I need touch input example 1 Answer

[HELP] Joystick Movement/Button Movement 0 Answers

on touch play animation in android devices. 1 Answer

iOs/Android Development 2 Answers

How to get position of touch on touch screen 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