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 LeftyTwoGuns · Sep 24, 2013 at 01:57 AM · transformbooleantransformdirection

Using TransformDirection incorrectly?

I'm trying to set up a bool identifying which direction on the X-axis a vehicle is moving (right or left). I figured TransformDirection is how I would go about doing this. Yet I get this error: "An object reference is required to access non-static member 'UnityEngine.Transform.TransformDirection(UnityEngine.Vector3)' ". Here is my script:

     private bool goingLeft;
     private bool goingRight;
 
 void Update();
 
 goingRight = Transform.TransformDirection(Vector3.right);
 goingLeft = Transform.TransformDirection(Vector3.left);
         
         if(goingRight){
             Debug.Log("Going right!");
         }
         
         if(goingLeft){
             Debug.Log("Going left!");
             }
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 clunk47 · Sep 24, 2013 at 02:14 AM 2
Share

First off, don't use capital T on transform when you are referring to the component. Second, transform.TransformDirection is a Vector3, not a bool. Bool is a true / false. Before I can actually answer this, I need to know, how are you moving your object??? Is it a rigidbody? Or are you moving it with Translate or Position+?

avatar image LeftyTwoGuns · Sep 24, 2013 at 02:20 AM 2
Share

The vehicle is a rigidbody that only moves left or right on the X-axis (it's a 2D perspective) and it's moved with motorTorque and Wheel Colliders

avatar image clunk47 · Sep 24, 2013 at 03:33 AM 1
Share

Thanks for clarifying.

2 Replies

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

Answer by clunk47 · Sep 24, 2013 at 02:28 AM

Since it's a rigidbody, check out Rigidbody.velocity.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class example : MonoBehaviour
 {
     bool goingLeft;
     bool goingRight;
      
     void Update()
     {    
         if(rigidbody.velocity.x > 0)
             goingRight = true;
         else
             goingRight = false;
         if(rigidbody.velocity.x < 0)
             goingLeft = true;
         else
             goingLeft = false;
         
         
         if(goingRight)
             Debug.Log("Going right!");
          
         if(goingLeft)
             Debug.Log("Going left!");
     }
 }
Comment
Add comment · Show 6 · 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 LeftyTwoGuns · Sep 24, 2013 at 03:09 AM 0
Share

I've tried Rigidbody.velocity, it works accordingly but when the vehicle brakes the log will show GoingLeft/Right alternating continuously.

The vehicle's chassis does bounce and wobble due to the spring settings of wheel colliders (which is intended), could this be the cause? And will it affect the use of this script?

avatar image clunk47 · Sep 24, 2013 at 03:23 AM 1
Share

Try adjusting the velocity statement. Ins$$anonymous$$d of > or < than Zero, try 1.0f, 0.5f, 5.0f, depending on the max velocity, just experiment around with the velocity statement to find what speed you need to check to deter$$anonymous$$e left or right, starting with smaller numbers and moving up accordingly.

avatar image LeftyTwoGuns · Sep 24, 2013 at 03:26 AM 2
Share

Alright, thanks I'll mess around some more with it. You pretty much answered my question, if I have further difficulties with the vehicle I'll just ask a new question. Thank you to everyone else, as well

avatar image LeftyTwoGuns · Sep 24, 2013 at 06:47 PM 2
Share

Just as an update, tinkering the value of the velocity statement results in the desired affect. Thanks again!

avatar image clunk47 · Sep 24, 2013 at 06:49 PM 1
Share

Sweet! Glad I could help man :)

Show more comments
avatar image
2

Answer by robertbu · Sep 24, 2013 at 02:07 AM

'Transform' is the class. You want the 'transform' which is the transform of the specific game object. Note there are some shortcuts. 'transform.right' is the same as transform.TransformDirection(Vector3.right). And for left, you can use '-transform.right'.

Your next problem is that Transform.TransformDirection() returns a Vector3, not a bool, and it does not return the direction you are heading. How will solve this problem will depend on how you are moving the object. If you object is facing in the direction of movement, you can use either Vector3.Dot() or Vector3.Angle() and compare Vector3.left and Vector3.right to figure out which direction it is going. If the object is not rotated, then you will have to calculate the velocity by subtracting the position of the previous frame. Checking the sign of the 'x' axis of the velocity will give you the direction the object is moving.

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

16 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

Related Questions

transform.position not executed with OnTrigger2D 1 Answer

Transform All Instances Of A Prefab 1 Answer

Accessing boolean properties from array of transforms 1 Answer

Using PositionHandle to set a a directional vector, using TransformDirection causing editor freakouts. Help! 0 Answers

Boolean a Array of Transforms 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