Hello.
I am trying to make a simple physics game and throw some gameObjects. I want to pick up a box with mouse cursor and throw it. I googled, and found a piece of code which works technically, but only with OnMouseDown() it is left click. But I want right click :D!
Problem with the script: It is not registering right click at all, I probably messed up. I’m just a beginner (3th day).
This is what I have for now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObj : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseDrag()
{
if (Input.GetMouseButtonDown(1))
{
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
}
}
}
@priit1989
I have created a decent example script for you to further expand and it has comments to explain what is going on. So I hope it helps.
[Header("Needed to get the mouse position of the player")]
public Camera cam;
[Header("How far a player can hit an object from")]
public float reachOfPlayer = 15;
[Header("Player's throw force of objects | PS: THE OBJECT BEING THROWN MUST HAVE RIGIDBODY COMPONENT TO WORK")]
public float throwForce = 15;
[Header("This will display our currently selected object")]
public GameObject selectedObject = null;
[Header("Offset of x, y, and z from the player")]
public Vector3 offset = new Vector3(1, 1, 1);
[Header("Speed our selected object moves towards our grabbed position")]
public float speed = 30;
//Variables:
private bool clicked = false;
private bool holding = false;
private Transform selectedObjectsParent;
private void Update()
{
//If left clicked
if (Input.GetMouseButtonDown(1))
{
//Perform our click function
ClickedOnObject();
//If we are holding the object and it is not null and it has been clicked
if (holding && selectedObject != null && clicked)
{
//We are throwing the object so set the parent back to its original parent
selectedObject.transform.parent = selectedObjectsParent;
//Let our object rotate all it wants now
selectedObject.GetComponent<Rigidbody>().freezeRotation = false;
//Make sure our object can move freely now
selectedObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
//Get its rigidbody and throw it in a forward direction from our player with our players throw force
selectedObject.GetComponent<Rigidbody>().AddRelativeForce(transform.forward * throwForce, ForceMode.Impulse);
//We are no longer selecting the object so set it to null
selectedObject = null;
//We are not holding our object so set holding to false
holding = false;
//Our object is no longer selected so set clicked to false
clicked = false;
}
}
//If we are currently selecting an object and we clicked it
if (selectedObject != null && clicked)
{
//If our object is not throwable then we can not grab it
if (selectedObject.GetComponent<Rigidbody>() == null)
{
//We are no longer selecting the object so set it to null
selectedObject = null;
//We are not holding our object so set holding to false
holding = false;
//Our object is no longer selected so set clicked to false
clicked = false;
return;
}
//Lets parent the selected object we our holding to our player so it follows our rotation
selectedObject.transform.parent = transform;
//Make sure our object does not rotate around
selectedObject.GetComponent<Rigidbody>().freezeRotation = true;
//Make sure our object stays held in the air
selectedObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
//Set our selected object to follow our players position with a set offset
Vector3 newObjectPosition = new Vector3(transform.position.x + offset.x, transform.position.y + offset.y, transform.position.z + offset.z);
selectedObject.transform.position = Vector3.MoveTowards(selectedObject.transform.position, newObjectPosition, speed * Time.deltaTime);
//Now we know we are for sure holding the selected object so set holding to true
holding = true;
}
}
void ClickedOnObject()
{
//Will hold the reference for the clicked object
RaycastHit hit;
//Find the position and direction from the mouse
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//Using the direction and origin from the mouse(ray)
//Output the hit object (out hit)
//Max Distance the ray can go until it hits an object(reachOfPlayer)
if (Physics.Raycast(ray, out hit, reachOfPlayer))
{
//If the hit object has not been clicked
if (clicked == false)
{
//Set our selected object
selectedObject = hit.transform.gameObject;
//Save our selected objects original parent
selectedObjectsParent = selectedObject.transform.parent;
//Make sure we set our object as clicked
clicked = true;
}
}
}