Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by IgnitedGames · May 10, 2019 at 01:09 AM · transformtransform.positiontransform.rotationtransform.rotate

Issues with copying Y axisrotation of another object

Sorry about my lack of knowledge in advance, I’m not great at using Unity. I’m trying to make a script that copies the X, Y and Z position and the Y rotation of another object. Here’s my script: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class FollowSphere : MonoBehaviour
 {
     public Transform playerTransform
     public GameObject self; //this is the object the script is attached to
     public Quaternion newRotation;
     void Update()
     {
         self.transform.position = playerTransform.position //this actually works
         newRotation = new Quaternion(0f, playerTransform.rotation.y, 0f, 0f) // I have no idea what I am doing
         self.transform.rotation.Set(newRotation); //I really have no idea what I am doing
     }
 }

Somebody please help me :( I’d appreciate if you could redo the code so it’s more efficient, but if you’re going to do so please explain what each different line does so I know for the future. Once again, sorry about my lack of knowledge and thank you for your help!

Comment
Add comment · Show 1
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 IgnitedGames · May 10, 2019 at 04:10 AM 0
Share

Bump am I allowed to bump?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darkStar27 · May 10, 2019 at 05:23 AM

Here, try this

 /*
  * Script Summary:
  * Copies the Transform (Position & Rotation) of one GameObject(source) to Other(target)
  */
 using UnityEngine;
 
 /// <summary>
 /// Identifies the type of cordinates to be replicated
 /// </summary>
 public enum CordinateType {
     Local,
     Global
 };
 public class ReplicateTranslationAndRotaion : MonoBehaviour {
 
     [Tooltip("Drag here the GameObject whose transform is to be COPIED FROM.")]
     public GameObject sourceGameObject;
     [Tooltip("Drag here the GameObject whose transform is to be CHANGED TO.")]
     public GameObject targetGameObject;
 
     /// <summary>
     /// Equates Position and Rotation of two GameObjects (both Local and Global)
     /// </summary>
     /// <param name="sourcePosition">Transform of the GameObject to COPY VALUES FROM</param>
     /// <param name="targetPosition">Transform of the GameObject to COPY VALUES TO</param>
     /// <param name="cordinateType">Defines the Cordinate</param>
     public void EquateTransform (Transform sourcePosition, Transform targetPosition, CordinateType cordinateType) {
         if (cordinateType == CordinateType.Local)
         {
             targetPosition.position = sourcePosition.position;
             targetPosition.rotation = sourcePosition.rotation;
         }
         if (cordinateType == CordinateType.Global)
         {
             targetPosition.localPosition = sourcePosition.localPosition;
             targetPosition.localRotation = sourcePosition.localRotation;
         }
     }
 }

Hope this Helps!

Comment
Add comment · Show 4 · 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 darkStar27 · May 10, 2019 at 05:26 AM 0
Share

One more thing just call the Equate transform function from somewhere,

if you want to it to run once call it from Start() or Update() if want to run it every frame..

 EquateTransform (sourceGameObject.transform, targetGameObject.transform, CordinateType.Global);
 

Reply if there's any problem..!

avatar image IgnitedGames darkStar27 · May 10, 2019 at 07:06 AM 0
Share

Thank you for the reply! The only issue is with the Rotation I only want it to copy the Y axis, not the Z and X. Will this code do this?

avatar image IgnitedGames IgnitedGames · May 10, 2019 at 07:22 AM 0
Share

$$anonymous$$ore Info: I thought that adding a rigidbody component and freezing the X and Z Rotation would stop it, but it didn't for some dumb reason. I also had it working before with this code: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class FollowSphere : $$anonymous$$onoBehaviour{
     public Transform playerTransform;
     public Transform self;
     void Update(){
         self.position = playerTransform.position;
         transform.rotation = Quaternion.AngleAxis(playerTransform.rotation.y, Vector3.up);
     }
 }

But now it doesn't work. What is happening rn

Show more comments
avatar image
0

Answer by Shreka · May 10, 2019 at 08:30 AM

Did u try using eularangles...

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

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

139 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 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 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 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 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 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.rotate 4 Answers

Possible way to improve performance moving large hierarchies? 1 Answer

How Do I Give Two Objects the Same Transform? 1 Answer

Coroutine - transform for every frame for duration? 0 Answers

Adding quaternions using a seperate axis 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