• 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
Question by phatality123 · May 17, 2015 at 08:31 PM · androidraycast3dtouchray

Touch to move

Hi. I'm trying to conduct a little project for android but I've reached an issue. I'm trying to have my character move along a terrain so I made it to where if you click somewhere, a marker appears at the location and your character automatically follows it, as t$$anonymous$$s script does:

 function Update () {
      for (var i = 0; i < Input.touchCount; i++) {
          if (Input.GetTouch(i).phase == TouchPhase.Moved) {
              var touchPos : Vector3 = Input.GetTouch(i).position;
              touchPos.z = 4.0;
              touchPos.y *= 4.0;
              var createPos = myCam.ScreenToWorldPoint(touchPos);
              projectile.position = createPos;
          }
      }

I also set up 2 cameras; one that follows the player and another that is at a top-down angle facing the terrain and character (w$$anonymous$$ch is the one that is referenced to in the script), both of w$$anonymous$$ch are orthograp$$anonymous$$c. My problem is that I can't seem to get the touch marker to move/conform along the terrain, or if there's a $$anonymous$$gh area the marker would just go through it, as it doesn't change its Y value if part of the terrain is raised. How would I go about accurately pinpointing the touch location in relation to the 3d environment? I've tried placing the marker $$anonymous$$gh up and raycasting downwards but the touch part wouldn't respond at all, or very poorly.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by redeemer · May 18, 2015 at 05:38 AM

I'd suggest to use raycast to detect the exact point where the tpuch and the terrain collides.

Somet$$anonymous$$ng like should work :

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 
 RaycastHit[] $$anonymous$$ts = Physics.RaycastAll(ray, 200);

 foreach(RaycastHit $$anonymous$$t in $$anonymous$$ts) {
                 
     if($$anonymous$$t.collider.CompareTag("Terrain")) Debug.Log($$anonymous$$t.point);
 
 }

Instead the mouse position, use your touch position.

Of course you could set the raycast to only detect the layers you want, t$$anonymous$$s will detect the terrain point even if you touch over other object as long as the terrain is in the ray.

Hope t$$anonymous$$s helps

Comment
phatality123
CrandellWS

People who like this

2 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 phatality123 · May 18, 2015 at 10:53 PM 0
Share

I'm kind of confused; for this approach, should I keep my 2-cam setup or would this work for my main camera?

avatar image redeemer · May 19, 2015 at 03:09 AM 0
Share

That code uses the main camera Camera.main.ScreenPointToRay(Input.mousePosition), but you can use the camera you want.

For example :

 Camera topCam = GameObject.FindGameObjectWithTag("TopCam");
 
  Ray ray = topCam.ScreenPointToRay(Input.mousePosition);
                  
  RaycastHit[] hits = Physics.RaycastAll(ray, 200);
  
  foreach(RaycastHit hit in hits) {
                  
      if(hit.collider.CompareTag("Terrain")) Debug.Log(hit.point);
  
  }

Take a look at this http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

In your case, I guess your following camera is the main cam, so if you want to click in that cam use the first code, if you want to click in the "mini-map" or "top-down" cam, use the second one.

Of course, you terrain must have a collider since you're not using the Unity Terrain in order to being detetcted by the raycast.

avatar image phatality123 · May 19, 2015 at 08:03 PM 0
Share

Did I get this right (using your prior suggestion) because after rewriting it in javascript I get no errors though nothing is printed. I made sure my objects are tagged as well.. var rays : Ray; rays = mainCamera.ScreenPointToRay(Input.mousePosition); var hits : RaycastHit[];
hits = Physics.RaycastAll(rays, 200);

  for(var hit : RaycastHit in hits) {
                  
      if(hit.collider.CompareTag("Terrain")) 
      {
           print(hit.point);
      }

Testing it with mouse first just to see if it works.

avatar image redeemer · May 19, 2015 at 10:46 PM 0
Share

I've made you two examples, one with a cube used as the terrain and another with a Unity Terrain. Hope this help you solve any doubts. I leave you the link to the unitypackage

https://mega.co.nz/#!i1NlFJgJ!fToUf5WbZuwTiZd03NU0eXx_6pYmqVKc5FHvlF0Qcss

avatar image phatality123 · May 20, 2015 at 01:14 AM 0
Share

Thanks :). You've been really helpful.

Show more comments
avatar image

Answer by hanger · May 18, 2015 at 06:14 AM

I've never used it, but what you want to do is query the terrain height at a particular world position. If you're using the Unity terrain system, there's a call for that.

Comment

People who like this

0 Show 1 · 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 phatality123 · May 18, 2015 at 07:49 PM 0
Share

Sorry, maybe I should've specified that my terrain was made manually in blender.

avatar image

Answer by abhi_360 · May 18, 2015 at 06:09 AM

Your touchPos has no height component i.e the Input.GetTouch(i).position provides the ScreenSpace positions w$$anonymous$$ch is (pixelWidth,pixelHeight) like a vector2 so when your changing screen to world point that function has no height to it w$$anonymous$$ch then sets to zero you can Debug.Log(createPos).

You can use Physics RayCasts for t$$anonymous$$s job. But if you have Unity 5 then you can download there standard assets w$$anonymous$$ch already has the kind of controller (like strategy games Civilization, Dota).

Comment

People who like this

0 Show 4 · 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 phatality123 · May 18, 2015 at 07:41 PM 0
Share

So in what way should I try setting up the raycasts, because I've used them but am experiencing issues with my current setup.

avatar image abhi_360 · May 19, 2015 at 05:07 AM 0
Share

if you can be more specific about your raycast problems then i can give a solution. Like what issues are you experiencing

avatar image phatality123 · May 19, 2015 at 08:22 PM 0
Share

Going into areas with accessible overhangs where the ray is interrupted, having the ray-casting follow touch/click location somewhat accurately, if at all. My current setup has a gameobject, high up, following the touch position in relation to the top-down camera (not the main one following player) raycasting downwards and at the hitpoint is a separate gameobject (a marker).

avatar image abhi_360 · May 20, 2015 at 09:29 AM 0
Share

good you found your solution

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to touch select 3D objects 2 Answers

Is there a way to toggle between active characters via a GUI? 0 Answers

Problems with Raycast/AddForce on a Ball in 3D 1 Answer

Raycast on touch 3 Answers

Using raycasts to move a game object toward touch point. 0 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