- Home /
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!
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!
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..!
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?
$$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