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 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 this 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 (which is the one that is referenced to in the script), both of which are orthographic. My problem is that I can't seem to get the touch marker to move/conform along the terrain, or if there's a high 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 high up and raycasting downwards but the touch part wouldn't respond at all, or very poorly.

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
2
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.

Something like should work :

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

 foreach(RaycastHit hit in hits) {
                 
     if(hit.collider.CompareTag("Terrain")) Debug.Log(hit.point);
 
 }

Instead the mouse position, use your touch position.

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

Hope this helps

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 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 "$$anonymous$$i-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_6pYmqV$$anonymous$$c5FHvlF0Qcss

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

Thanks :). You've been really helpful.

Show more comments
avatar image
0

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
Add comment · 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
0

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 which is (pixelWidth,pixelHeight) like a vector2 so when your changing screen to world point that function has no height to it which then sets to zero you can Debug.Log(createPos).

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

Comment
Add comment · 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. $$anonymous$$y 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.

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

Menu issues - selecting [BACK] closes app 1 Answer

Limit the position of the object, but still keep following the touch? How to use ClampMagnitude properly? 1 Answer

Button Focus Prevents Touch Raycast (Android C#) 1 Answer

is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers

Obtain a vector3 point in space from a ray hit 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