- Home /
C# Find Which Axes Gameobject is on
I have two gameobjects. One functions as a reference for the axes. The other one will have script attached to it that will determine what axis it is lying on. Any idea how I could find that info? I've tried if(someGameObject2.transform.position.y == someGameObject.transform.position.y) but that if statement is comparing values and someGameObject's axes could have the exact same value such as (0,0,0). I have some pseudo code written up. I'm just not sure what I want to compare to see if one gameobject is on the y axis of another gameobject
using UnityEngine;
using System.Collections;
public class AxisChecker : MonoBehaviour {
public GameObject someGameObject;
public GameObject someGameObject2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
/*if(someGameObject2 is lying on someGameObject.transform.x)
Debug.Log("someGameObject2 is lying on someGameObject's x axis");
if(someGameObject2 is lying on someGameObject.transform.y)
Debug.Log("someGameObject2 is lying on someGameObject's y axis");
if(someGameObject2 is lying on someGameObject.transform.z)
Debug.Log("someGameObject2 is lying on someGameObject's z axis");
*/
}
}
Not sure exactly what you are trying to do. A picture would help.
Ins$$anonymous$$d of lying on I should have said collide with. Is there a way I can draw the x,y and z axes of a gameobject as raycasts?
As mentioned in the answer (which now based on this comment may not be at all what you are looking for), transform.forward, transform.up and transform.right are the world directions for the forward, right and up sides of your game object. You can use these in a Raycast.
Answer by robertbu · Jul 23, 2014 at 02:48 AM
The transform has three vectors you can use: transform.up, transform.right and transform.forward. These axes are the world space direction for the forward, right and up side of your game object. To figure out what axis an object as aligned with another object, you can use Vector3.Angle() or Vector3.Dot() for the dot product, if two normalized vectors are aligned, it returns a value of 1.0. If they are exactly opposite, then it returns a value of -1.0. I'm going to make an assumption here based on your wording and that you care about the 'y' (up) axis. So you can do:
if (Mathf.Abs(Vector3.Dot(someObject.transform.up someObject2.transform.up) > 0.9f) {
Debug.Log("Aligned on the 'Y' axis");
}
else if (Mathf.Abs(Vector3.Dot(someObject.transform.forward someObject2.transform.up) > 0.9f) {
Debug.Log("Aligned on the 'Z' axis");
} else if (Mathf.Abs(Vector3.Dot(someObject.transform.right, someObject2.transform.up) > 0.9f) {
Debug.Log("Aligned on the 'Z' axis");
}
The .0.9 is an arbitrary threshold, and 'someObject' is the reference object. The use of Abs() here means that two objects can be aligned in either direction.
Your answer
Follow this Question
Related Questions
C# Rotate GameObjects Regardless of List Size 2 Answers
C# Change Script Help 1 Answer
C# SetActive GameObject Array 2 Answers
C# GameObject Reverses Z Rotation 0 Answers