Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating Problem!

Discussion in 'Scripting' started by Wister1230, Jun 29, 2015.

  1. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    Hello all,
    I tried to do that if i moving the mouse it will move the object to the mouse place but it works bad in the rotating it seems like i rotating around the GameObject and not the GameObject rotating with me!
    Here is an example:
    --> this video from 3:06 mins!


    And here is how it works for me:


    Here the Codes:
    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.E))
    2.         {
    3.             Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0.0f));// creating ray to the middle of the screen.
    4.             RaycastHit hit;
    5.  
    6.             if(Physics.Raycast(ray, out hit, 500.0f))
    7.             {
    8.                 if(IsPlacing == false)//Cant place another one while placing!
    9.                 {
    10.                     IsPlacing = true;
    11.                     PlacingObject = Instantiate(Resources.Load("Objects/Foundation"), new Vector3(hit.point.x, hit.point.y +2, hit.point.z), Quaternion.Euler(0, Camera.main.transform.rotation.y, 0)) as GameObject;
    12.                     PlacingMaterial = Resources.Load("Materials/AblePlacement", typeof(Material)) as Material;
    13.                     PlacingObject.GetComponent<Renderer>().material = PlacingMaterial;
    14.                     //PlacingObject = null;//Sets the things only on the last object, and then deleting the variable!
    15.                 }
    16.                 else if(IsPlacing == true)
    17.                 {
    18.                     Debug.Log ("Finish placing to countinue!");
    19.                 }
    20.  
    21.             }
    22.         }
    23.         if (IsPlacing == true)
    24.         {
    25.             Vector3 MousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Distance);
    26.             Vector3 PlacingObjectPosition = Camera.main.ScreenToWorldPoint(MousePosition);
    27.             PlacingObject.transform.position = PlacingObjectPosition;
    28.             PlacingObject.transform.position = Vector3.Lerp (PlacingObject.transform.position, PlacingObject.transform.position, Smoothy);
    29.             PlacingObject.transform.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * CrusorSpeed, 0);
    30.         }
    How can i fix this problem and make it the same as the first video?
    Thanks all for help!
     
    Last edited: Jun 30, 2015
  2. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    Can you please try to debug PlacingObject after line 29 ?
     
  3. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
  4. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    Are you sure that you are using Vector3.Lerp correctly?
     
  5. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    I can delete it, i checked it and it doesnt help me, the main problem is with the rotation i want to do that if the rotation saved it doesnt metter where i will move the GameObject it will show me the rotation degress in the game because it looks like i am rotating around the object and not the object rotates with me...
     
  6. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    And why are you placing Input.GetAxis("Mouse ScrollWheel") * CrusorSpeed on Y ? do you want it to rotate Up or right/left ?
     
  7. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    I wanna rotate it right and left..
     
  8. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    I made some searching try this out:

    replace

    Input.GetAxis("Mouse ScrollWheel") * CrusorSpeed

    with

    Input.GetAxis("Mouse Y") * CrusorSpeed

    Please tell me if it worked.
     
  9. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    It is not working good bro.
     
  10. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    That's strange , really.. what are you getting now?
     
  11. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    When i`m moving the mouse up and down it rotates the object.
     
  12. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
  13. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    It rotates the object not in the way i want it to rotate.