I need to drag object with axis direction Like unity editor how to made this thank you.
public class DragAxis : MonoBehaviour
{
// Start is called before the first frame update
Vector3 faceDirection;
GameObject target;
void Update()
{
FindObject();
DragObject();
}
void DragObject()
{
if(target != null)
{
Vector2 delta = Mouse.current.delta.ReadValue();
var angle = Mathf.Atan2 (delta.y, delta.x) * Mathf.Rad2Deg;
Vector3 direction = faceDirection * angle;
target.transform.position += direction.normalized* delta.magnitude*Time.deltaTime;
}
}
void FindObject()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out RaycastHit hit))
{
faceDirection = hit.normal;
target = hit.collider.gameObject;
}
}
if(Input.GetMouseButtonUp(0))
{
target = null;
}
}
}
Here my code still not working
If I understand correctly you want to move an object, when you click with your mouse on a specific axis, like an elevator door, is that right ?
something like that,I need to control object on one axis same as drag object with transform axis in unity editor
Very good, so only on one axis if I understood correctly.
I have several ideas, but someone has already created a similar script, and distributes it for free, I leave it to you to study this script.
you put the script on the gameObject of your choice on your scene, and you create the “drag” tag on your object to move, that’s it.
You will just have to change the axis in your code if necessary.
Script :
Tuto youtube :
Good luck
I think it can also help you to understand
EDIT :
No answer, I hope that suits you ?
In the link above in the Unity forum LiterallyJeff posted some code in his first answer which is quite simple to understand, you can embed it simply by copying it into a new script in the Updates tags, then you create a condition with a GetMouseButtonDown input, a bool and you have your drag and drop axis in simplified mode;)
EDIT 2 :
I don’t know what you intend to do with these scripts, but don’t forget that you can freeze the position/rotation of a rigidbody if you need.
If you need more explanation don’t hesitate to ask, but I think that with these two scripts you have plenty to do.
Thank you bplc. I think still not the way I want.
i will try to code but wrong way to drag.