- Home /
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;
}
}
Edit: I caught the hit.point.y on line 61, its changed now but still not working
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);
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
Follow this Question
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