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 HuskyPanda213 · Feb 14, 2014 at 06:11 AM · transformbuildingrounding

Rounding transform.position? With parents

How do I round my object(a buildable), to the nearest say .25, .5 or 1(does not matter which), in position(x,y,z), and the rotation to the nearest 30 degrees, I know this could be achieved with Mathf.Round() but how do I make it that it does not mess up the local position/rotation. It is a buildable which is a gameobject that in in a fp controller("Main Camera/Tools/TheObject" to be exact). It also has a move up/down command, which also I need help making it work with the global position.

So I guess the main question is how do I make the global position/rotation round, without messing up the local position/rotation.

I guess another way to word this is: The local position is 0,0,0 and the global is not known, the local rotation is -90,0,0 and global is not known, how would I round the global without messing up the local, and still having the object infront a play, but with a rounded transform.

Code(if needed):

 using UnityEngine;
 using System.Collections;
 
 public class Buildable : MonoBehaviour {
 
     public PlayerItemManager itemManager;
 
     private float holdTimer = 0;
 
     public float objectYPosOffset = 0;
 
     void Start(){
         itemManager = transform.root.GetComponent<PlayerItemManager> ();
         objectYPosOffset = -2.3f;
     }
 
     void OnEnable(){
         itemManager = transform.root.GetComponent<PlayerItemManager> ();
         objectYPosOffset = -2.3f;
     }
 
     void Update(){
         ManageInput ();
         RoundObjectTransform ();
         ManageBuilding ();
     }
 
     private void ManageInput(){
         if (Input.GetKeyUp (KeyCode.PageUp)) {
             objectYPosOffset += 0.25f;
         }
         if (Input.GetKeyUp (KeyCode.PageDown)) {
             objectYPosOffset -= 0.25f;
         }
     }
 
     private void ManageBuilding(){
         if (Input.GetMouseButton (1)) {
             holdTimer+=Time.deltaTime;
             if(holdTimer > 1){
             holdTimer = 0;
             RoundObjectTransform();
             PlaceObject();
             }
         }
     }
 
     private void RoundObjectTransform(){
         Vector3 pos = transform.localPosition;
         pos.y = 1 + objectYPosOffset;
         transform.localPosition = pos;
         Vector3 rot = transform.eulerAngles;
         rot.x = -90;
         transform.eulerAngles = rot;
         //transform.rotation.x = -90;
     }
 
     private void PlaceObject(){
         GameObject go = Resources.Load ("Buildables/"+gameObject.name) as GameObject;
         GameObject obj = Network.Instantiate (go,transform.position,transform.rotation,0) as GameObject;
         itemManager.UseCurrentSlot (true);
 
     }
 
 }
Comment
Add comment · Show 2
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 Imankit · Feb 14, 2014 at 08:55 AM 0
Share

Look if u change the global position of an object then its local position will change for sure. If you want the local position not to change then keep this object in an empty gameobject and change the empty gameobject's global position. In this way your object's localposition will be intact.

avatar image HuskyPanda213 · Feb 14, 2014 at 06:35 PM 0
Share

Ok, Thanks!

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

Rounding not working? 2 Answers

Advice on reducing rounding errors with transform 1 Answer

Need to switch objects 0 Answers

get nearest game 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