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 Dragonfly3r · Feb 06, 2015 at 01:52 PM · c#raycastclick and drag

Click & Drag change from xy to xz

So I've done up a script with allows the player to click and drag objects with the mouse currently at the moment it moves on the XY axis however I want to make it so the object can only be set to move on the XZ axis. How would I go about doing this? I also have made an snap to ground script so that if the object collides with another object it will snap to the surface of it.

MouseScript

 using UnityEngine;
 using System.Collections;

 public class MouseScript : MonoBehaviour {

 private RaycastHit rayHit;
 private GameObject collideObj;
 private float distance;
 private Vector3 posObj;
 private bool lockObj;
 private int layerMask;

 // Use this for initialization
 void Start () 
 {
     layerMask =~ (1 << 8);
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (Input.GetMouseButton (0)) 
     {
         var ray = camera.ScreenPointToRay (Input.mousePosition);
         var hit = Physics.Raycast (ray.origin, ray.direction, out rayHit, 100f, layerMask);
         if (hit && !lockObj) 
         {
             lockObj = true;
             collideObj = rayHit.collider.gameObject;
             collideObj.GetComponent<StickToGround>().enabled = false;
             distance = rayHit.distance;

             Debug.Log (collideObj.name);
         }
         if (lockObj) 
         {
             posObj = ray.origin + distance * ray.direction;
             collideObj.transform.position = new Vector3 (posObj.x, posObj.y, collideObj.transform.position.z);
         }
     }
     else 
     {
         lockObj = false;
         if (collideObj != null)
         {
             collideObj.GetComponent<StickToGround>().enabled = true;
             collideObj = null;
         }
     }
 }
 }

Stick to Ground Script

 using UnityEngine;
 using System.Collections;

 public class StickToGround : MonoBehaviour 
 {
 private RaycastHit rayHit;
 public float deltaPos;
 // Use this for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     var hit = Physics.Raycast(transform.position, transform.up * (-1), out rayHit);
     if (hit) 
     {
         transform.position = new Vector3(rayHit.point.x, rayHit.point.y + deltaPos, rayHit.point.z);
     }

 }
 }
Comment
Add comment · Show 3
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 Mmmpies · Feb 06, 2015 at 02:58 PM 0
Share

How are you stopping it from immediately clicking back down in place?

I'm having trouble visualizing what you're trying, I mean I understand you want to move objects into the scene rather than up, but I just can't think how it's going to work without instantly snapping to the ground if you don't allow for an initial move upwards.

You got a screen shot?

Incidentally the theory is just translate the y position of the mouse to be z distance from the camera but I still think it'll be more complex than that makes it sound.

avatar image Dragonfly3r · Feb 10, 2015 at 11:30 AM 0
Share

Well the script in question is being used to depict the pads from a defibrillator so stick to the ground script relates to the the pads being placed on the body. Too which at a later point I'll put notification that states either if the user has placed it in the correct spot or not.

I kind of know where the problem in question is as I'm controlling the object with ScreenPointToRay on the camera and as the camera is in a cone shape by moving the object towards the middle of the screen in turns brings the object high due to the cone like formation of the camera.

How would i go about forcing the Ray so that e.g. the camera was square sized or 2D e.g. it still used the Y value to revert back to as a step up. But it was still fixed in place so it could only run along the x & z axis?

avatar image Dragonfly3r · Feb 10, 2015 at 12:42 PM 0
Share

An idea has crossed my $$anonymous$$d on how to fix the problem but I haven't the foggiest how to implement it. $$anonymous$$y idea is to make a set of boundaries for the object when its moved by the mouse so e.g. the object can't move away from the boundaries of the camera or invisible barriers preventing the mouse from dragging the object past a certain point. If placed above the gameobject then it would prevent it from rising up in the cone shape of the camera and keep it at a fixed height. How would I implement this?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Creating 2d image map of objects based off 3d space colliders Unity C# 0 Answers

(Oculus Rift) Casting a ray from mouse through distortion matrix 1 Answer

Raycast won't fire 3 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