Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by DRProductions · Jul 19, 2014 at 02:51 AM · rotationtransformvector3quaternioneulerangles

c# modify only one axis of a quaternion

I know that in Js you can directly modify one axis of a quaternion or Vector3, in c# you cant. So usually you just do something like:

 void Update(){ 
 transform.rotation = new Quaternion.Euler(transform.rotation.eulerAngles.x,10,transform.rotation.eulerAngles.x);
 }
 

or something but the problem is that that still restricts another script from rotating the same object since it is constantly setting that objects rotation to the current rotation (Atleast that is the behavior I am getting). I am probably just overlooking something simple or being downright stupid.

Comment
Add comment
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

2 Replies

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

Answer by SirCrazyNugget · Jul 19, 2014 at 03:04 AM

If you only want to set one of the axes why not just create a public float property which can be accessed by any other script

 public float rotY {
     get { return transform.rotation.eulerAngles.y; }
     set {
         Vector3 v = transform.rotation.eulerAngles;
         transform.rotation = Quaternion.Euler (v.x, value, v.z);
     }
 }

(Or something similar to).Then you can access it by

 OtherGameObject.rotY = 45;

Comment
Add comment · Show 2 · 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 DRProductions · Jul 19, 2014 at 03:20 AM 0
Share

I cant seem to access the property. it says that rotY is not a member of gameobject EDIT. I fixed it by using

 gameObject.GetComponent<Simple$$anonymous$$ouseRotator>().rotX = 0;

However, now I get the error "Object reference not set to an instance of an object" at line 23

avatar image SirCrazyNugget · Jul 19, 2014 at 03:57 AM 0
Share

Create a script for the object being rotated

 //Rotatee.cs
 using UnityEngine;
 using System.Collections;
 
     public class Rotatee : $$anonymous$$onoBehaviour {
     
         public float rotY {
             get { return transform.rotation.eulerAngles.y; }
             set {
                 Vector3 v = transform.rotation.eulerAngles;
                 transform.rotation = Quaternion.Euler (v.x, value, v.z);
             }
         }
     
     }

Create a script for the object in charge of the rotating

 //Rotater.cs
 using UnityEngine;
 using System.Collections;
 
 public class Rotater : $$anonymous$$onoBehaviour {
 
     [Range(0, 360)]
     public float rotY;
 
     public Rotatee rotatee;
 
     void Update(){
         rotatee.rotY = rotY;
     }
 
 }


Assign the objects as required, place the rotatee in the rotater.roratee slot and play with the slider.

avatar image
1

Answer by larswik · Sep 04, 2017 at 06:14 AM

I know this is old but after 2 of my 3 day holiday I dedicated to working on my game I could not get my particle I used as shrapnel to explode in the same direction the canon ball was heading as it arc'd. Code that finally worked below. Thanks you very much!

             transform.rotation = Quaternion.LookRotation(testRigid.velocity);
             Vector3 v = transform.rotation.eulerAngles;
             transform.rotation = Quaternion.Euler (v.x, v.y, 90);
Comment
Add comment · Show 3 · 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 DageLV · Dec 27, 2019 at 01:57 PM 0
Share

Namespace v does not exist.

avatar image N-8-D-e-v DageLV · Jun 19, 2020 at 07:40 PM 0
Share

declare the vector3 called v

avatar image Boudewijn · Oct 02, 2020 at 08:50 AM 0
Share

This is very useful. Thanks!

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

25 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform a Vector by a Quaternion 1 Answer

Instantiate GameObject towards player 0 Answers

Turret rotation on one axis problems 2 Answers

Euler Angles and Vectors 3 Answers

Working on a mini map 0 Answers


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