- Home /
C#: Move an object along one axis at time
I have to recreate an orthogonal axis to be able to move objects along one axis at a time. I find some info but I stumbled upon the mainly javascript and I want realize all in C #. I continued to look but I take this opportunity to ask for some help :) Thank you in advance.
I try to do something with this piece of code but the line commented out references an error (Assets / Standard Assets / Scripts / Utility Scripts / _$$anonymous$$enus.cs (98.30): error CS1955: The member `UnityEngine.Transform. position 'Can not Be Used as delegate or method)
void On$$anonymous$$ouseDrag(GameObject curPlanche)
{
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.y = curPlanche.transform.position.y;
//curPlanche.transform.position(point.x,0,0);
Screen.showCursor = false;
}
The way it's written, it's trying to call the vector like a function. Position is a Vector3, not a function. Think you meant this...
curPlanche.transform.position = Vector3(point.x,0,0);
most likely willtati's code will give an error, you need to add a new in front of the Vector3. So:
curPlanche.transform.position = new Vector3( point.x, 0, 0 );
Also, if the object is not always going to be 0 on the y and z axes, then you can do:
Vector3 curPlanchePosition = curPlance.transform.position;
curPlanche.transform.position = ( point.x, curPlanchePosition.y, curPlanchePosition.z );
ack, sorry, picking up stupid javascript habits. Thanks for the correction, danny!
Answer by Kalu · Nov 12, 2011 at 08:06 PM
This code will perhaps be able to help someone else, it can manage an orthogonal axis. The last piece of code creates invisible planes (collider), which can continue to run the "OnMouseDrag" even if the cursor go out of the collider of the current arrow (the graduation).
if(leftClikDown == true){ //Left Click EnterFrame
if(curTarget != null && curPlanche != null){
if(switchControl == false){
if(curTarget.name == "Green"){//l'axe vert du repert orthonormé à été attrapé
OnMouseDrag(curPlanche);
}
else if(curTarget.name == "Red"){//l'axe rouge du repert orthonormé à été attrapé
OnMouseDrag(curPlanche);
}
else if(curTarget.name == "Blue"){//l'axe bleue du repert orthonormé à été attrapé
OnMouseDrag(curPlanche);
}
}
}
}
void OnMouseDrag(GameObject curPlanche)
{
//Initialisation du lancé de rayon (dans le monde 3D) depuis les coordonnées de la souris (x,y)
Ray localRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit localHit;
Vector3 localV3;
Vector3 localVp3;
Vector3 localVs3;
if (Physics.Raycast(localRay, out localHit, 50)){//si(lancé de rayon chope un truc dans les 50 unité droit devant)
float localDist = localHit.distance;
localV3 = localRay.GetPoint(localDist);//récupération du point(x, y, z) 'hiter'
localVp3 = repereOrtho.transform.position;//Positionne le repére orthonormé
if(curTarget.name == "Red"){ // axe X
repereOrtho.transform.position = new Vector3 (localV3[0], localVp3[1], localVp3[2]);
//Positionne la planche courante
localVs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (localVp3[0], localVp3[1]-(localVs3[1]/2), localVp3[2]);
}
if(curTarget.name == "Green"){ // axe Y
repereOrtho.transform.position = new Vector3 (localVp3[0], localV3[1], localVp3[2]);
//Positionne la planche courante
localVs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (localVp3[0], localVp3[1]-(localVs3[1]/2), localVp3[2]);
}
if(curTarget.name == "Blue"){ // axe Z
repereOrtho.transform.position = new Vector3 (localVp3[0], localVp3[1], localV3[2]);
//Positionne la planche courante
localVs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (localVp3[0], localVp3[1]-(localVs3[1]/2), localVp3[2]);
}
if(curTarget != null){ createDragPlane(curTarget); }
}
}
void createDragPlane(GameObject curAxe){//permet d'affiner la précision du controle de translation des planches
Vector3 localVp3; //limites les tremblements et les sauts
Quaternion localQ3;
Quaternion localQr3;
if(cube == null){
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);//créer un Cube
if(curAxe.name == "Red"){ cube.transform.localScale = new Vector3(20, 20, .01f);
}else if(curAxe.name == "Green"){
cube.transform.localScale = new Vector3(20, 20, .01f);
cube1 = Instantiate(cube, cube.transform.position, Quaternion.identity) as GameObject;//Instancie un nouveau Cube
cube1.transform.localScale = new Vector3(.01f, 20, 20);
cube1.renderer.enabled = false;//rend le cube1 invisible
}else if(curAxe.name == "Blue"){ cube.transform.localScale = new Vector3(.01f, 20, 20); }
cube.renderer.enabled = false;
if(curAxe.name != "Green"){
cube2 = Instantiate(cube, cube.transform.position, Quaternion.identity) as GameObject;
cube2.transform.localScale = new Vector3(20, .01f, 20);
cube2.renderer.enabled = false;
}
}
localVp3 = curTarget.transform.position;
localQ3 = cube.transform.rotation;
localQr3 = curTarget.transform.localRotation;
if(cube != null){
cube.transform.position = new Vector3 (localVp3[0], localVp3[1], localVp3[2]);
cube.transform.rotation = Quaternion.Slerp(localQ3, localQr3, 0);
}
if(cube1 != null){
cube1.transform.position = new Vector3 (localVp3[0], localVp3[1], localVp3[2]);
cube1.transform.rotation = Quaternion.Slerp(localQ3, localQr3, 0);
}
if(cube2 != null){
cube2.transform.position = new Vector3 (localVp3[0], localVp3[1], localVp3[2]);
}
}
Answer by Kalu · Nov 06, 2011 at 07:53 PM
I work on this piece of code but I do not understand why the debug.log on the "point" return always the same thing (at enterFrame) when I move the mouse in any direction ...
void OnMouseDrag(GameObject curPlanche)
{
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.x = curPlanche.transform.position.x;
Debug.Log("point.x = "+ point.x+" point.y = "+point.y+" point.z = "+point.z);
//Positionne le repére orthonormé
Vector3 V3 = repereOrtho.transform.position;
repereOrtho.transform.position = new Vector3 (point.x, V3[1], V3[2]);
//Positionne la planche courante
Vector3 Vs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (point.x, V3[1]-(Vs3[1]/2), V3[2]);
}
So, nothing happens. But I would like to undo this problem to see what happens ...
Answer by Kalu · Nov 06, 2011 at 09:02 PM
I came to this code there. There are still two problems: When moving along an axis, the origin of the orthonormal frame is attached to the cursor. When moving along the Y axis, if the ray crosses (without touching the arrow of the Y axis) it will touch the ground and positioned the "repereOrtho" at y = 0.
void OnMouseDrag(GameObject curPlanche)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector3 V3;
if (Physics.Raycast(ray, out hit, 50)){
float dist = hit.distance;
V3 = ray.GetPoint(dist);
//Positionne le repére orthonormé
Vector3 Vp3 = repereOrtho.transform.position;
if(curTarget.name == "Red"){ // axe X
repereOrtho.transform.position = new Vector3 (V3[0], Vp3[1], Vp3[2]);
//Positionne la planche courante
Vector3 Vs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (Vp3[0], Vp3[1]-(Vs3[1]/2), Vp3[2]);
}
if(curTarget.name == "Green"){ // axe Y
repereOrtho.transform.position = new Vector3 (Vp3[0], V3[1], Vp3[2]);
//Positionne la planche courante
Vector3 Vs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (Vp3[0], Vp3[1]-(Vs3[1]/2), Vp3[2]);
}
if(curTarget.name == "Blue"){ // axe Z
repereOrtho.transform.position = new Vector3 (Vp3[0], Vp3[1], V3[2]);
//Positionne la planche courante
Vector3 Vs3 = curPlanche.transform.localScale;
curPlanche.transform.position = new Vector3 (Vp3[0], Vp3[1]-(Vs3[1]/2), Vp3[2]);
}
}
}
I find (i think) a solution to the problem of ray tracing that crosses the orthonormal graduation by positioning invisible plan (collider), and I also removed the collider of ground. Ray tracing should not get lost ... must try it..