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 beatrage · Oct 23, 2010 at 05:34 PM · mousetouchfollow

How to make the object player followsmooth2D the mouse or the touch?

I am thinking in buying unity but i am being beaten up by a simle coding task that is to create all types of controlls for the iphone. I have completed the joypad tutorial BUT i am having a real hard time trying to find the DAM mouse positions and realocate that on the position of my object that controls the SmoothFollow2D.js script ( as I really don't use java I ask for a resonable answer to this simple task in C sharp ) a task sooo simple that in itorque i can make it, in Gamesalad i can make it ( in 1 minute) but in unity I can't!!

I am seeing that unity is very flexible and transparent in many things but in Input it lacks ( very hard ) tutorials especially regarding touch and accelerometer functionalities!!

A simple code like this ( the object that my player will lock the SmoothFollow2D.js) :

using UnityEngine; using System.Collections;

public class MousePosition : MonoBehaviour { private float y; private float x;

void Update() { if (Input.GetMouseButtonDown(0)) { y = Input.mousePosition.y; x = Input.mousePosition.x; }

transform.Translate(x, y, Time.deltaTime);

}

}

and then after I choose my player to follow this object I can see that my player follows him, but when i click on the screen to test if i can make it follow my click the object goes up-right and stays there?

What am I doing wrong here?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lhk · Oct 24, 2010 at 01:46 PM

The mouse position is given in Screen coordinates. Therefore you have two values x and y. However the y in the Scene regards the heigth of your objects - unless you have turned everything by 90 degrees. You should use the following:

    void Update() {
  if (Input.GetMouseButtonDown(0))
  {
    y = Input.mousePosition.y;
    x = Input.mousePosition.x;
  }
  Vector3 move=new Vector3(x,0,y);
  move*=movespeed;
  move*=Time.deltaTime;
  transform.Translate(move);}

As you see the mouse's y coordinate maps to the world's z coordinate.

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 beatrage · Oct 26, 2010 at 06:32 PM

Thanks for the answer my friend but, this code makes my object goes to the right position of the screen and stays there!Also it doesn't update the inputs, strange my code above :

using UnityEngine; using System.Collections;

public class MousePosition2 : MonoBehaviour {

private float x, y; private float movespeed = 10;

void Update() { if (Input.GetMouseButtonDown(0)) { y = Input.mousePosition.y; x = Input.mousePosition.x; } Vector3 move = new Vector3(x,0,y); move*=movespeed; move*=Time.deltaTime; transform.Translate(move);

}

}

My camera is an Orthographic camera so I can only make 2D games ( for while of course ) but the problem here is that as I am making games for iphone I wanted not to depend only on touches but also use the Input.mousePosition to acess the screen input positions.

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 Casey · Dec 17, 2010 at 02:27 PM

you could try casting a ray from your finger position onto a surface and then moving the object to that position.

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

No one has followed this question yet.

Related Questions

Touch command for GUIText on Android? OnMouseDown() for Android? 1 Answer

Camera follow mouse around the map 2 Answers

Mouse move with specific speed? 2 Answers

OnMouseDown is checked when build for Android 2 Answers

Convert Touch input to Mouse input C#? 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