• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ajna · Sep 07, 2014 at 06:38 PM · c#movementitweenmouseclick3rd person camera

Itween, how to mouseclick movement on 3rd person camera

alt text

So, that's the clear image of my upcoming project, actually a task.

My next goal s to make the player (cube) go to the position where the mouse clicks on the board. Diablo Style. I'm having a little trouble trying to do the Mouse Pick since the Input.mousePosition operates in a (x,y) screen area. Tried to sum the camera position but it's not totally accurate, and still adds Y even if I set it to 0.

Searc$$anonymous$$ng on iTween I've found - even without any explanation anywhere - the 'movement on uneven terrain', w$$anonymous$$ch is not precisely needed, since the floor if flat. Anyways, how can I do it?

The so far no-good code:

 if (Input.GetMouseButtonDown(0))
 {
      Vector3 point = new Vector3 (Input.mousePosition.x + cam.transform.position.x,
      0, Input.mousePosition.z + cam.transform.position.z);
      var ray = Camera.main.ScreenPointToRay(point);
      float ent = 10.0f;
      Vector3 $$anonymous$$tPoint = ray.GetPoint(ent);

      iTween.MoveTo(target, $$anonymous$$tPoint, 1);
 }

ps: It's a squared but not a tiled board, the player can move freely on it.

07-09 13-39.png (300.3 kB)
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
0

Answer by Lorthan · Sep 07, 2014 at 07:59 PM

There are some ways you can do It.

I'd personally rather make it follow an empty Gameobject, the mouseclick only sets the empty Gameobject's position and then use the MoveBy from ITween, makes it feel more organic.

That way you don't need to use ScreenPointToRay but rather ScreenToWorldPoint.

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 robertbu · Sep 07, 2014 at 08:49 PM

Standard forms of converting between screen and world coordinates like Camera.ScreenToWorldPoint() will not work with an angled camera. A Raycast() is the usual solution. Use a Plane.Raycast() for an infinite or very large plane with no obstructions. A Collider.Raycast() works well with a limited plane without without obstructions. Physics.Raycast() can handle obsructions. Here is an untested rewrite of your code above to use Plane.Raycast():

 if (Input.GetMouseButtonDown(0)) {
      Plane plane = new Plane(Vector3.up, transform.position);
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      float ent = 0.0f;
      if (Plane.Raycast(ray, out ent))  {
           Vector3 $$anonymous$$tPoint = ray.GetPoint(ent);
           iTween.MoveTo(gameObject, $$anonymous$$tPoint, 1f);
      }
 }

T$$anonymous$$s code goes on the block.

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 Ajna · Sep 08, 2014 at 12:22 AM 0
Share
avatar image robertbu · Sep 08, 2014 at 02:39 PM 0
Share
avatar image Ajna · Sep 08, 2014 at 04:26 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Multiple Cars not working 1 Answer

Problem with iTween and instantiated path 0 Answers

Problem with three lane movement 0 Answers

Unity3d 4,script nGUI btn drag convert to move where i mouse click 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