Move object away or closer while dragging with mouse
This is my first post and i m new to Unity so i ' m sorry if i do something wrong.
Im working on a script to move an object with the mouse. The requierments are to:
Move the object with the mouse
To rotate the object
The object must interact with other object (collision)
To move the object away or closer to the camera while dragging it with the mouse.
I'm following this script that i found: https://sharpcoderblog.com/blog/drag-rigidbody-with-mouse-cursor-unity-3d-tutorial
i have made some adjustments and added a way to rotate the mouse, this is what i have so far:
public class MoveObject : MonoBehaviour
{
#region Properties
public float forceAmount = 1000;
private Rigidbody selectedRigidbody;
private Camera targetCamera;
private Vector3 originalScreenTargetPosition;
private Vector3 originalRigidbodyPos;
private float selectionDistance;
//my try with key movement
/*------------------------------*/
private Vector3 verticalMovement;
/*---------------------------------*/
#endregion
#region Events
// Start is called before the first frame update
void Start()
{
targetCamera = GetComponent<Camera>();
verticalMovement = new Vector3();
}
void Update()
{
if (!targetCamera)
return;
if (Input.GetButtonDown("Fire 1"))
{
//Revisa si al presionar se tiene un Rigidbody
selectedRigidbody = GetRigidbodyFromMouseClick();
}
if (Input.GetButtonUp("Fire 1") && selectedRigidbody)
{
//Libera al objeto que se esta moviendo
verticalMovement = new Vector3();
selectedRigidbody.velocity = new Vector3();
selectedRigidbody = null;
}
if (Input.GetButtonUp("Rotation") && selectedRigidbody)
{
//Rotacion del Objeto en el eje y
var yRotation = 0.0f;
if (Input.GetAxis("Rotation") > 0) yRotation = 90.0f;
if (Input.GetAxis("Rotation") < 0) yRotation = -90.0f;
selectedRigidbody.transform.Rotate(0, yRotation, 0);
}
}
void FixedUpdate()
{
if (selectedRigidbody)
{
/*-------------------------------------*/
if (Input.GetButton("Vertical"))
{
//movimiento vertical de objeto
if (Input.GetAxis("Vertical") > 0)
{
verticalMovement = targetCamera.transform.forward;
}
else
{
verticalMovement = -targetCamera.transform.forward;
}
}
/*--------------------------------------------------*/
Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;
Debug.Log(mousePositionOffset);
selectedRigidbody.velocity = (originalRigidbodyPos + mousePositionOffset - selectedRigidbody.transform.position + verticalMovement) * forceAmount * Time.deltaTime;
//Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;
//selectedRigidbody.velocity = (originalRigidbodyPos + mousePositionOffset - selectedRigidbody.transform.position) * forceAmount * Time.deltaTime;
}
}
#endregion
#region Methods
Rigidbody GetRigidbodyFromMouseClick()
{
RaycastHit hitInfo = new RaycastHit();
Ray ray = targetCamera.ScreenPointToRay(Input.mousePosition);
bool hit = Physics.Raycast(ray, out hitInfo);
if (hit)
{
if (hitInfo.collider.gameObject.GetComponent<Rigidbody>())
{
selectionDistance = Vector3.Distance(ray.origin, hitInfo.point);
originalScreenTargetPosition = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance));
originalRigidbodyPos = hitInfo.collider.transform.position;
return hitInfo.collider.gameObject.GetComponent<Rigidbody>();
}
}
return null;
}
#endregion
}
i added the private Vector3 verticalMovement;
and the targetCamera.transform.forward
to try to move the object with the w (foward) and s (back) key. The object does move but i think the mouse offset does not update the distance position so the movement does not work that well.
Basic information about the "project"
Imagine a room where you are standing in the middle of it and you can only rotate the camera (with the mouse), you can move objects (there are multiple) around to build something like a living room.
basically i was wondering what i could do to make it work? or if the way i ' m handling this is incorrect and there's a much better way of doing this?
Your answer
Follow this Question
Related Questions
How to make a Smooth grid based movement with mouse/touch drag 0 Answers
Input.GetAxis going crazy when I click outside the game window 0 Answers
How can I use both mouse and keyboard to do the same thing? [SOLVED] 1 Answer
Using MoveTowards to move GameObject from starting point to mouse cursor 0 Answers