- Home /
Make Object snap to position within Trigger area when mouse button released
Im using a simple script to pick up and move an object using the mouse/cursor.
Vector3 dist;
float posX;
float posY;
void OnMouseDown()
{
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
}
void OnMouseDrag()
{
Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}
Now I want to place the object in one of three places (A, B, C,)when the mouse is released. Each drop point is a transform(parent) with a box collider(child) as trigger that is larger than the carried objects. Height isn't an issue as all game objects are same height. I need the carried object to be placed at the transform as long as the mouse button was released while the object was within the Trigger. edit - (think CCG/TCG when a card is dropped and jumps to an empty slot)
This was my most recent attempt, no errors, but not working?
public class putCardInPlay : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Card")
{
if (Input.GetButtonUp("Fire1"))
{
Debug.Log("Item Dropped onto Position");
//Object no loger Controlled by Mouse
//Move Object onto Place Holder ?
}
}
}
}
Hopefully that explanation made sense? Any help greatly appreciated.
$$anonymous$$aybe lerp the object towards the place with trigger? Your code makes sense to me. Does the Debug.Log work?
Are you making a 2D or 3d game? and did you make sure that one of the objects has a RigidBody? Also did you make sure that the tag is on the child (the object with the collider)? because I see that you have a parenting situation, and it might get confusing sometimes.
try "$$anonymous$$ouse0" ins$$anonymous$$d of "Fire1" ??
Has anyone found out the answer to this?,Has Anyone found out the answer for this?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My spikes don't damage the player 2 Answers
Raycast Snapping Problem. 0 Answers