Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 andycodes · Apr 13, 2017 at 04:37 AM · raycastpositiononmousedrag

Dragging object in constrained area

I'm creating a game where the player starts a level by placing the golf ball in the placement box, so I draw a ray and if it hits a square with a tag "StartGreen", then it places the object at that place, but offset by the balls height so its on top.

That all works Fine and dandy. However, I want it so that the player can drag the ball around the box as long as they haven't lifted the mouse. to do this, I traced more rays while on Mouse Drag and if the ray hit within the box, then the ball position is updated.

My Problem: The object position is not updated while being dragged, is my understanding of how often onmouseDrag() incorrect, can i not call rays (should i not) in this function? if not could i go about this a better way? (maybe using bounds of the tee box?)

By the way: this.gameobject is the tee box which has this script attached, and onMouseUp works.

My Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlaceGolfBall : MonoBehaviour {
 
     public Material onPadMat;
     public Material offPadMat;
     public GameObject itemInstance;
     bool goodHit = false;
     RaycastHit hit;
 
     float yPos;
    
 
     // Use this for initialization
     void Start () {
         gameObject.GetComponent<MeshRenderer>().material = offPadMat;
     }
     
     // Update is called once per frame
 
     //places the ball initially
     private void OnMouseDown()
     {
         if (!enabled) return;
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit)) {
             if (hit.transform.gameObject.tag == "StartGreen" && itemInstance != null)
             {
                 yPos = hit.point.y + itemInstance.GetComponent<MeshRenderer>().bounds.extents.y;
 
                 Vector3 position = new Vector3(hit.point.x, yPos, hit.point.z);
 
                 Instantiate(itemInstance, position, Quaternion.identity);
                
 
                 goodHit = true;
                 //gameObject.GetComponent<MeshRenderer>().material = onPadMat;
                 //debug.log("<color=green>placed successfully! script deactivated.</color>");
                 //this.enabled = false;
             }
             else {
                 Debug.Log("<color=red>Unsuccessful Placement</color>");
             }
         }
         
     }
 
     //if was placed, then can be dragged as long as the mouse is down.
     private void OnMouseDrag()
     {
         if (!enabled) return;
 
         if (goodHit) {
             print("wasGood");
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit)) {
                 if (hit.transform.gameObject.tag == "StartGreen") {
                     itemInstance.transform.position = new Vector3(hit.point.y, yPos, hit.point.z);
                 }
             }
         }
     }
 
     //deactivates the script now that the placement is over.
     private void OnMouseUp()
     {
         if (!enabled) return;
 
         gameObject.GetComponent<MeshRenderer>().material = onPadMat;
         Debug.Log("<color=green>placed successfully! script deactivated.</color>");
         this.enabled = false;
 
     }
 
 }
Comment
Add comment · Show 1
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 andycodes · Apr 13, 2017 at 04:52 AM 0
Share

Edit: I caught the hit.point.y on line 61, its changed now but still not working

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by andycodes · Apr 13, 2017 at 05:03 AM

I found the solution! the object being passed to change position was the reference to the prefab, therefore the object placed would not be changed.

Line 12: Gameobject placedObject;

Line 36: placedObject = Instantiate(itemInstance, position, Quaternion.identity);

Line 61: placedObject.transform.position = new Vector3(hit.point.x, ypos, hit.point.z);

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 andycodes · Apr 13, 2017 at 05:05 AM 0
Share

If anyone objects to using Ray Tracing on$$anonymous$$ouseDown(), please leave a comment so i can update the answer with more efficient code!

Ps for anyone trying this, Set the Ball to Ignore Raycasting at least while being placed, you'll avoid issues with the ball looking jittery.

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

93 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move player with mouse help 0 Answers

Moving object with raycast issues 2 Answers

RayCast relative to camera stays in 1 place. 2 Answers

how to tell what platform you're on. 1 Answer

Is it possible to cast a ray but ignore Z position? 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