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 MithosAnnar · Mar 08, 2012 at 08:52 PM · movementtouch

Touch movement not working

I am trying to move a gameObject by touch.

     #pragma strict
 
 var player : GameObject;
 var moveToPos : Vector3;
 var touchPos : Vector3;
 var myTransform : Transform;
 var moveSpeed : int;
 
 function start ()
 {
    myTransform = transform;
 }
 
 function Update ()
 {
    touchPos = Input.mousePosition;
 
    player.transform.position.z = 5;
       
    if (Input.GetMouseButton(0)) {
    
    moveToPos = Vector3 (touchPos.x , myTransform.position.y , 5);   
 
    myTransform.Translate(moveToPos * moveSpeed * Time.deltaTime);
    
    Debug.Log(touchPos);
 
 }
 }

But no matter where I touch the object zooms to the right. I have asked someone with more experienced than me in unity and he has no idea why it isn't working. I also noticed that i had to set all the variable's values manually in the inspector.

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 MithosAnnar · Mar 09, 2012 at 03:07 PM 0
Share

I imported meshes from silo.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by StephanK · Mar 09, 2012 at 11:04 AM

Input.mousePosition returns ScreenCoordinates in pixels. Those are always > 0. So the x value of your velocity vector (the one you use to translate) will always be > 0 hence you move to the right.

What you have to do is convert the ScreenCoordinates to WorldCoordinates. You can do that using Camera.ScreenToWorldPoint.

Comment
Add comment · Show 6 · 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 MithosAnnar · Mar 14, 2012 at 01:54 PM 0
Share

I have done a lot of editing and have come up with this:

var moveSpeed : int = 10; var mousePos : Vector3;

function Update () {

transform.position.z = 2;

mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

if(Input.Get$$anonymous$$ouseButtonDown(0)) {

transform.Translate (Vector3 (10, 9, 2));

} }

However when i click the object moves randomly around the screen. I have consistently encountered this problem when trying to translate my object to a Vector3.

avatar image mpavlinsky · Mar 14, 2012 at 03:11 PM 0
Share

It seems like given that code it would translate the object (10,9,2) no matter where you clicked, now wouldn't it?

avatar image MithosAnnar · Mar 14, 2012 at 03:33 PM 0
Share

@mpavlinsky, No. It doesn't even translate there. when I tell it to move to (10, 9, 2) it moves where ever it wants to. And I have no idea why.

avatar image MithosAnnar · Mar 14, 2012 at 03:35 PM 0
Share

Is it a problem with transform.Translate?

I can't translate to anywhere I specify. Let alone my mouse position!

avatar image mpavlinsky · Mar 14, 2012 at 03:47 PM 0
Share

Translate moves the object by whatever you pass in, not to it. This is my understanding from the script reference at least, I have never actually used this function.

You probably want transform.position = whatever;

Show more comments

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

7 People are following this question.

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

Related Questions

EARTH CALLING ANSWER!!!! 1 Answer

Trying to get the player to face the direction of a touch 1 Answer

How could I implement diagonal movement in this code? 0 Answers

Touch and slide GUI box & grid selected buttons cSharp 1 Answer

Clamping Issue 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