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 JimRoot · Apr 14, 2014 at 05:45 PM · cameraraycastdrag

Drag Camera only when raycasting Terrain

Hey! I have a Simple Draggable Camera Script

 using UnityEngine;
 using System.Collections;
 
 public class ViewDrag : MonoBehaviour {
     Vector3 hit_position = Vector3.zero;
     Vector3 current_position = Vector3.zero;
     Vector3 camera_position = Vector3.zero;
     float z = 0.0f;
     
     // Use this for initialization
     void Start () {
         
     }
     
     void Update(){
         if(Input.GetMouseButtonDown(0)){
             hit_position = Input.mousePosition;
             camera_position = transform.position;
             
         }
         if(Input.GetMouseButton(0)){
             current_position = Input.mousePosition;
             LeftMouseDrag();        
         }
     }
     
     void LeftMouseDrag(){
         current_position.z = hit_position.z = camera_position.y;
 
         Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
         
         // Invert direction to that terrain appears to move with the mouse.
         direction = direction * -1;
         
         Vector3 position = camera_position + direction;
         
         transform.position = position;
     }
 }

and i wanted to ask, how can i change the script in a way, that it is only reacting, when the mouse is hitting the layer "Floor"?

my problem is that i have this draggable camera script and a NGUI scroll panel in my game. so every time i drag the scroll panel, the whole world/camera is also dragging

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by karma0413 · May 31, 2015 at 11:46 AM

I believe the function you seek is Physics.Raycast

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

One of the variables you can use in the RayCast statement is filtering it through layers... http://docs.unity3d.com/Manual/Layers.html

So from a design perspective, I would:

--put my UI on a new layer called "UI"

--put my Terrain on a new layer called "Terrain"

--Determine if mousebuttondown(0) == true

--if it is true, then use the physics.raycast to cast a ray from the camera to the 3dworld space, but also using the filter settings to check for BOTH: UI and Terrain

Now define it a little further..........

--if the out HitInfo.collider.gameObject.layer was the UI layer - then only adjust the scrolling UI

--if the out HitInfo.collider.gameObject.layer was the terrain layer - then use your camera dragging code

Let me know if you have confusion in setting up LayerMask or the Physics.Raycast code....

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

21 People are following this question.

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

Related Questions

GameObject Moving Slow when drag with hand 0 Answers

Camera raycast and rotated collider 0 Answers

Make the terrain ignore Raycast if in between Camera and Player? 1 Answer

Raycast Object Selection 3 Answers

Help with Raycast on Render Texture 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