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 Ochreous · Jul 23, 2014 at 02:11 AM · c#gameobjectaxes

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");
         */
     }
 }
 
Comment
Add comment · Show 3
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 Kiwasi · Jul 23, 2014 at 02:23 AM 1
Share

Not sure exactly what you are trying to do. A picture would help.

avatar image Ochreous · Jul 23, 2014 at 02:47 AM 0
Share

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?

avatar image robertbu · Jul 23, 2014 at 02:49 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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

22 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

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

C# Gameobject Rigidbody Mouse Collision 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