• 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 darthvaderxd · Jun 06, 2014 at 10:59 PM · objectfacing

how to change the facing direction of one object when it hit the wall?

I have one cube that move in only one direction. when it hit the wall, he move back again. it just go and come infinetely. but when he get back he comes backward. how can i make it rotate to face the other wall? this is the script that i'm using to move the cube #pragma strict

var direction = Vector3(0, 0, 1);

function Start () {

}

function Update () {

} function OnCollisionEnter (hit : Collision) { var tag = hit.gameObject.tag;
if (tag == "wall")

    direction.z *= -1;
 

}

thanks in advance :)

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

1 Reply

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

Answer by oasisunknown · Jun 07, 2014 at 12:43 AM

you can try holding the Z value in a variable and change that.

 var direction = 1; //direction of faceing
 moveDirection = Vector3(0,0,direction); // Direction of travel.
 
 //then in the OnCollisionEnter call
 
 direction *= -1; // changes the direction of facing.
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 oasisunknown · Jun 07, 2014 at 12:45 AM 0
Share

moveDirection may need to be a new Vector3 I am not positive on when and when not to use new. (thats my research project to night I guess)

avatar image oasisunknown · Jun 07, 2014 at 01:01 AM 0
Share

Upon further thinking about this. are you sure your not wanting to flip the object the other direction because the Vector3 is referring to its translation. where if you want the cube to basically flip 180 degrees then in that case you would basically do the same code but ins$$anonymous$$d of a Vector3.

I got you a reference video from the unity archives.
the part where he is flipping his character based on key inputs is at $$anonymous$$ute 39. just taylor the code to fit your needs and make it flip when it hits the wall and not based on keys.

http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

(also note I know your working in 3d and the video is on 2d but the concept should be similar. if its not I am sure one of the more seasoned programmers will let us know where we both are going wrong)

avatar image darthvaderxd · Jun 09, 2014 at 02:58 AM 0
Share

Hello oasisunknown, sorry by my delay to answer. i'm watching the video you have suggested to me and i will try to adapt that to my game. Thanks for the help, I'll get back later to say if it's working :)

avatar image darthvaderxd · Jun 09, 2014 at 10:43 PM 0
Share

You believe that this simple script solve my problem? I just change the line "direction.z *= -1;" for "transform.Rotate(0,180,0);" I put this here, maybe someone is having the same problem. :)

pragma strict

/ This is our direction we're travelling in. var direction = Vector3(0, 0, 1);

// $$anonymous$$ove it along its direction. function Update () { transform.Translate(direction 7 Time.deltaTime); }

// If we hit a left or right wall, invert x direction. function OnCollisionEnter (hit : Collision) { var tag = hit.gameObject.tag;
if (tag == "parede") transform.Rotate(0,180,0);

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Moving an object in the direction its facing in the editor 1 Answer

how to check if an object is facing an other 2 Answers

object enters trigger: how to: calculate where it's facing: and continue to calculate while in trigger 1 Answer

Get the needed rotation to face other object 1 Answer

[js] How To Check If The Player Is Facing An Object 3 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