Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kalu · Nov 05, 2011 at 06:23 PM · gameobjectaxismove

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kalu · Nov 05, 2011 at 06:28 PM 0
Share

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;
 }
avatar image WillTAtl · Nov 05, 2011 at 06:53 PM 0
Share

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);

avatar image dannyskim · Nov 05, 2011 at 08:08 PM 0
Share

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 );

avatar image WillTAtl · Nov 05, 2011 at 08:15 PM 0
Share

ack, sorry, picking up stupid javascript habits. Thanks for the correction, danny!

avatar image dannyskim · Nov 05, 2011 at 08:40 PM 0
Share

$$anonymous$$y pleasure

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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]);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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]);
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kalu · Nov 08, 2011 at 11:13 PM 0
Share

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..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can I move a component from one GameObject to another in script? 1 Answer

How to code anywhere below -10? 1 Answer

Restricting "Drag GameObject" script to Z Axis? 1 Answer

Create Animation in unity3d only on one axis 1 Answer

Move another object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges