Locking rotation axis using a scriptVolatile vs. Interlocked vs. lockWhy is lock(this) … bad?Activity restart on rotation AndroidPrevent screen rotation on AndroidRotating videos with FFmpegHow does lock work exactly?Rotate already rotated object around axisset 2 axis on rotationWandering AI in unity C#Unity 3D - rotating gameobject without rotating axis

How can I train a replacement without them knowing?

Can sulfuric acid itself be electrolysed?

How could Tony Stark wield the Infinity Nano Gauntlet - at all?

Why do balloons get cold when they deflate?

From France west coast to Portugal via ship?

9 hrs long transit in DEL

Earliest evidence of objects intended for future archaeologists?

Control GPIO pins from C

Why was ramjet fuel used as hydraulic fluid during Saturn V checkout?

Levenshtein Neighbours

Why should I pay for an SSL certificate?

Have made several mistakes during the course of my PhD. Can't help but feel resentment. Can I get some advice about how to move forward?

Nicely-spaced multiple choice options

Reducing contention in thread-safe LruCache

Rotate List by K places

Why is the name Bergson pronounced like Berksonne?

Do predators tend to have vertical slit pupils versus horizontal for prey animals?

Why do aircraft leave cruising altitude long before landing just to circle?

Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)

Installing certbot - error - "nothing provides pyparsing"

Peterhead Codes and Ciphers Club: Weekly Challenge

Will some rockets really collapse under their own weight?

What's the point of writing that I know will never be used or read?

What does a comma signify in inorganic chemistry?



Locking rotation axis using a script


Volatile vs. Interlocked vs. lockWhy is lock(this) … bad?Activity restart on rotation AndroidPrevent screen rotation on AndroidRotating videos with FFmpegHow does lock work exactly?Rotate already rotated object around axisset 2 axis on rotationWandering AI in unity C#Unity 3D - rotating gameobject without rotating axis






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a building mechanic in my game and when you press the build button the prefab follows your mouse and when you click it places there. The problem is it rotates according to what the mouse is on, for example if my mouse is on a tree side the object will rotate so its not clipping inside and it will rotate to match the tree
I hope this works



I've tried using a rigid body to lock to rotation and I've also tried using a rotation constraint but that didn't do anything.



using UnityEngine;
using UnityEngine.AI;

public class GroundPlacementController : MonoBehaviour

[SerializeField]
private GameObject placeableObjectPrefab;
public NavMeshObstacle nav;




[SerializeField]
private KeyCode newObjectHotkey = KeyCode.A;

private GameObject currentPlaceableObject;


private float mouseWheelRotation;






private void Update()

HandleNewObjectHotkey();
nav = GetComponent<NavMeshObstacle>();
if (currentPlaceableObject != null)

MoveCurrentObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();






private void HandleNewObjectHotkey()

if (Input.GetKeyDown(newObjectHotkey))

if (currentPlaceableObject != null)

Destroy(currentPlaceableObject);


else

currentPlaceableObject = Instantiate(placeableObjectPrefab);










private void MoveCurrentObjectToMouse()

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))

currentPlaceableObject.transform.position = hitInfo.point;

currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
var yourGridSize = 2.2f;

var currentPosition = currentPlaceableObject.transform.position;
currentPlaceableObject.transform.position = new Vector3(((currentPosition.x - (currentPosition.x % yourGridSize)) / yourGridSize) * yourGridSize,
((currentPosition.y - (currentPosition.y % yourGridSize)) / yourGridSize) * yourGridSize,
((currentPosition.z - (currentPosition.z % yourGridSize)) / yourGridSize) * yourGridSize);

currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;
if (currentPlaceableObject.name == "roof_pyramid")

print("hi");








private void RotateFromMouseWheel()

Debug.Log(Input.mouseScrollDelta);
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
print(mouseWheelRotation * 90f + "rotation");



private void ReleaseIfClicked()

if (Input.GetMouseButtonDown(0))


currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = true;
currentPlaceableObject.transform.Rotate(0, mouseWheelRotation, 0);
print("disabled");
currentPlaceableObject = null;
print("removed prefab");













share|improve this question
































    0















    I have a building mechanic in my game and when you press the build button the prefab follows your mouse and when you click it places there. The problem is it rotates according to what the mouse is on, for example if my mouse is on a tree side the object will rotate so its not clipping inside and it will rotate to match the tree
    I hope this works



    I've tried using a rigid body to lock to rotation and I've also tried using a rotation constraint but that didn't do anything.



    using UnityEngine;
    using UnityEngine.AI;

    public class GroundPlacementController : MonoBehaviour

    [SerializeField]
    private GameObject placeableObjectPrefab;
    public NavMeshObstacle nav;




    [SerializeField]
    private KeyCode newObjectHotkey = KeyCode.A;

    private GameObject currentPlaceableObject;


    private float mouseWheelRotation;






    private void Update()

    HandleNewObjectHotkey();
    nav = GetComponent<NavMeshObstacle>();
    if (currentPlaceableObject != null)

    MoveCurrentObjectToMouse();
    RotateFromMouseWheel();
    ReleaseIfClicked();






    private void HandleNewObjectHotkey()

    if (Input.GetKeyDown(newObjectHotkey))

    if (currentPlaceableObject != null)

    Destroy(currentPlaceableObject);


    else

    currentPlaceableObject = Instantiate(placeableObjectPrefab);










    private void MoveCurrentObjectToMouse()

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo))

    currentPlaceableObject.transform.position = hitInfo.point;

    currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
    var yourGridSize = 2.2f;

    var currentPosition = currentPlaceableObject.transform.position;
    currentPlaceableObject.transform.position = new Vector3(((currentPosition.x - (currentPosition.x % yourGridSize)) / yourGridSize) * yourGridSize,
    ((currentPosition.y - (currentPosition.y % yourGridSize)) / yourGridSize) * yourGridSize,
    ((currentPosition.z - (currentPosition.z % yourGridSize)) / yourGridSize) * yourGridSize);

    currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;
    if (currentPlaceableObject.name == "roof_pyramid")

    print("hi");








    private void RotateFromMouseWheel()

    Debug.Log(Input.mouseScrollDelta);
    mouseWheelRotation += Input.mouseScrollDelta.y;
    currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
    print(mouseWheelRotation * 90f + "rotation");



    private void ReleaseIfClicked()

    if (Input.GetMouseButtonDown(0))


    currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = true;
    currentPlaceableObject.transform.Rotate(0, mouseWheelRotation, 0);
    print("disabled");
    currentPlaceableObject = null;
    print("removed prefab");













    share|improve this question




























      0












      0








      0








      I have a building mechanic in my game and when you press the build button the prefab follows your mouse and when you click it places there. The problem is it rotates according to what the mouse is on, for example if my mouse is on a tree side the object will rotate so its not clipping inside and it will rotate to match the tree
      I hope this works



      I've tried using a rigid body to lock to rotation and I've also tried using a rotation constraint but that didn't do anything.



      using UnityEngine;
      using UnityEngine.AI;

      public class GroundPlacementController : MonoBehaviour

      [SerializeField]
      private GameObject placeableObjectPrefab;
      public NavMeshObstacle nav;




      [SerializeField]
      private KeyCode newObjectHotkey = KeyCode.A;

      private GameObject currentPlaceableObject;


      private float mouseWheelRotation;






      private void Update()

      HandleNewObjectHotkey();
      nav = GetComponent<NavMeshObstacle>();
      if (currentPlaceableObject != null)

      MoveCurrentObjectToMouse();
      RotateFromMouseWheel();
      ReleaseIfClicked();






      private void HandleNewObjectHotkey()

      if (Input.GetKeyDown(newObjectHotkey))

      if (currentPlaceableObject != null)

      Destroy(currentPlaceableObject);


      else

      currentPlaceableObject = Instantiate(placeableObjectPrefab);










      private void MoveCurrentObjectToMouse()

      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


      RaycastHit hitInfo;
      if (Physics.Raycast(ray, out hitInfo))

      currentPlaceableObject.transform.position = hitInfo.point;

      currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
      var yourGridSize = 2.2f;

      var currentPosition = currentPlaceableObject.transform.position;
      currentPlaceableObject.transform.position = new Vector3(((currentPosition.x - (currentPosition.x % yourGridSize)) / yourGridSize) * yourGridSize,
      ((currentPosition.y - (currentPosition.y % yourGridSize)) / yourGridSize) * yourGridSize,
      ((currentPosition.z - (currentPosition.z % yourGridSize)) / yourGridSize) * yourGridSize);

      currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;
      if (currentPlaceableObject.name == "roof_pyramid")

      print("hi");








      private void RotateFromMouseWheel()

      Debug.Log(Input.mouseScrollDelta);
      mouseWheelRotation += Input.mouseScrollDelta.y;
      currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
      print(mouseWheelRotation * 90f + "rotation");



      private void ReleaseIfClicked()

      if (Input.GetMouseButtonDown(0))


      currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = true;
      currentPlaceableObject.transform.Rotate(0, mouseWheelRotation, 0);
      print("disabled");
      currentPlaceableObject = null;
      print("removed prefab");













      share|improve this question
















      I have a building mechanic in my game and when you press the build button the prefab follows your mouse and when you click it places there. The problem is it rotates according to what the mouse is on, for example if my mouse is on a tree side the object will rotate so its not clipping inside and it will rotate to match the tree
      I hope this works



      I've tried using a rigid body to lock to rotation and I've also tried using a rotation constraint but that didn't do anything.



      using UnityEngine;
      using UnityEngine.AI;

      public class GroundPlacementController : MonoBehaviour

      [SerializeField]
      private GameObject placeableObjectPrefab;
      public NavMeshObstacle nav;




      [SerializeField]
      private KeyCode newObjectHotkey = KeyCode.A;

      private GameObject currentPlaceableObject;


      private float mouseWheelRotation;






      private void Update()

      HandleNewObjectHotkey();
      nav = GetComponent<NavMeshObstacle>();
      if (currentPlaceableObject != null)

      MoveCurrentObjectToMouse();
      RotateFromMouseWheel();
      ReleaseIfClicked();






      private void HandleNewObjectHotkey()

      if (Input.GetKeyDown(newObjectHotkey))

      if (currentPlaceableObject != null)

      Destroy(currentPlaceableObject);


      else

      currentPlaceableObject = Instantiate(placeableObjectPrefab);










      private void MoveCurrentObjectToMouse()

      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


      RaycastHit hitInfo;
      if (Physics.Raycast(ray, out hitInfo))

      currentPlaceableObject.transform.position = hitInfo.point;

      currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
      var yourGridSize = 2.2f;

      var currentPosition = currentPlaceableObject.transform.position;
      currentPlaceableObject.transform.position = new Vector3(((currentPosition.x - (currentPosition.x % yourGridSize)) / yourGridSize) * yourGridSize,
      ((currentPosition.y - (currentPosition.y % yourGridSize)) / yourGridSize) * yourGridSize,
      ((currentPosition.z - (currentPosition.z % yourGridSize)) / yourGridSize) * yourGridSize);

      currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;
      if (currentPlaceableObject.name == "roof_pyramid")

      print("hi");








      private void RotateFromMouseWheel()

      Debug.Log(Input.mouseScrollDelta);
      mouseWheelRotation += Input.mouseScrollDelta.y;
      currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
      print(mouseWheelRotation * 90f + "rotation");



      private void ReleaseIfClicked()

      if (Input.GetMouseButtonDown(0))


      currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = true;
      currentPlaceableObject.transform.Rotate(0, mouseWheelRotation, 0);
      print("disabled");
      currentPlaceableObject = null;
      print("removed prefab");










      c# unity3d rotation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 14:11









      Dom93

      1671 silver badge14 bronze badges




      1671 silver badge14 bronze badges










      asked Mar 27 at 13:54









      CriCri

      407 bronze badges




      407 bronze badges

























          0






          active

          oldest

          votes










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378954%2flocking-rotation-axis-using-a-script%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes




          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378954%2flocking-rotation-axis-using-a-script%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현