• 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
1
Question by chelnok · Apr 06, 2013 at 04:35 PM · directionvector

Check if the player going right direction

Hello, i want to add little helper to our racegame (like arrow or something), if the player is going wrong direction. Its two player splitscreen game. Attached image is overview of one level, and arrows represents desired direction; red for player1 and blue for player2.

I think this kind of thing can be done by checking player direction against some sort of direction vectors placed to the level. Not totally sure how to do that. Should i check if those two vectors intersect? Perhaps there is better way to accomplish this.. any thoughts?

alt text

Edit: checkpoint script added.

 #pragma strict
 
 static var curP1:String;
 var nextP1:String;
 static var _nextP1:String;
 
 static var curP2:String;
 var nextP2:String;
 static var _nextP2:String;
 
 
 function Start()
 {
     curP1 = "";
     curP2 = "";
     
     _nextP1 = "start";
     _nextP2 = "start";
 }
 
 
 function OnTriggerEnter (other : Collider) 
 {
     if ( other.gameObject.name == "Player1")
     {
         curP1 = transform.name;
     
         if (curP1 == _nextP1)
             _nextP1 = nextP1;
     }
     
     if ( other.gameObject.name == "Player2")
     {
         curP2 = transform.name;
         
         if (curP2 == _nextP2)
             _nextP2 = nextP2;
     }
 }



Idea is simple: every collider has a name and variable for next checkpoint. These needs to be edited in inspector, so i suppose i need to figure out better way to do this if i end up to use @CuddleMonster's idea.

directionhelper.jpg (10.9 kB)
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
2
Best Answer

Answer by CuddleMonster · Apr 06, 2013 at 04:53 PM

My Idea... assuming your cars/racers are a rigidbody: would be to have invisable boxes covering the entire track. They would be as wide as the track and as long as possible so long as there is no major curves in the track under them.

Each box would check to compare rigidbody.velocity (of your player) with the direction that the cube you are in says is forward. Have to run for now... Hopefully this helps.

PS. this could work for players going opposite directions by making a variable on the player that = rigidbody.velocity, except inversed on the players going in the opposite direction.

Comment
Add comment · Show 3 · 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 AlucardJay · Apr 06, 2013 at 05:54 PM 1
Share

yep, upvoted. Use trigger colliders as waypoints. There is an excellent example here : http://www.youtube.com/watch?v=7NehsLWcFIU

avatar image chelnok · Apr 07, 2013 at 03:08 PM 0
Share

Thanks for answer. At the moment i got just four checkpoints (+start) at the level; up,down,left,right. I'll try to add this method for those. Covering whole level with checkpoint system i have, would cause quite lot of work, so i suppose i should learn better way for checkpoints (waypoints), so thanks @alucardj for the example. I'll edit the question to include current checkpoint script.

avatar image chelnok · Apr 21, 2013 at 04:20 AM 0
Share

And for others, here is the code to check direction: if (Vector3.Dot(transform.forward, playerGO.rigidbody.velocity) < 0){ print("Wrong direction"); }

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

12 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

Related Questions

Need to know euler angles of Vector3 direction relative to other Vector3 direction 1 Answer

Calculate the General Direction of a Vector 3 Answers

Can't change vector direction 0 Answers

Why is my gun shooting backward? 2 Answers

How to fix/clamp rigidbody dir vector, despite Force dir 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