I need to drag objects during the game with Charp and mouse . Like you can see in the image below the objects are rotated in different position.
With the code below when i drag to Y both of them goes to new Y ok. When i drag the object B to his X it goes well to the new X, it goes exactly like the black arrows, But when i drag the object A, does not work good it drags again the object like the black arrow . I need to drag the object every time on his own axis like the axis show with the 3 colors (red, green, blue), especially i would prefer to drag the object on his x,y and to lock z axis.
Any idea ?
Any code sample ?
private bool _mouseState = false;
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
RaycastHit hitInfo;
target = GetClickedObject (out hitInfo);
_mouseState = true;
}
if (Input.GetMouseButtonUp (0)) {
_mouseState = false;
}
if (_mouseState) {
if (move) {
float xx = Input.GetAxis("Mouse X")/10;
float yy = Input.GetAxis("Mouse Y")/10;
target.transform.position = new Vector3(target.transform.position.x + xx, target.transform.position.y+yy,target.transform.position.z);
}
}
}
Hi aaro4130.
Thank you for post, but gives many errors, can you help with those ?
All the error is from line 18.
error CS1061: Type UnityEngine.Transform' does not contain a definition for left’ and no extension method left' of type UnityEngine.Transform’ could be found (are you missing a using directive or an assembly reference?)
error CS0019: Operator +' cannot be applied to operands of type float’ and UnityEngine.Vector3' error CS1502: The best overloaded method match for UnityEngine.Vector3.Vector3(float, float, float)’ has some invalid arguments
error CS1503: Argument #1' cannot convert object’ expression to type `floa
The problem is you were not changing the Z position. It needs to change X and Z to move it relative/local sideways, otherwise it will always move along World X.
To move it always to the right of the camera, you can change “target.transform.right” to “camerasTransform.right”, etc.
Thank you very much it works very well.
I notice that in order to move the object local need to change also the z, but i didnt know how to make it.
Now is perfect.