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;
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'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
add a comment |
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'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
add a comment |
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'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
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'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
c# unity3d rotation
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
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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