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 aman_jha · Jul 15, 2014 at 10:40 PM · c#raycastpositionupdate

Check if player object is right above this object

Hey Unitarians,

What I have is a player, and what I want is that when the player moves over a specific tile, the tile detects that the player is right above it, and freezes the player in the position until the user sets him free.

I've got the freezing all set up, but what I can't figure out yet is how exactly I can have the tile detect the player right above it. The player moves in the x and y axises. The z-axis remains constant for all objects in the scene. This is the code I have so far:

 if(transform.position.x == player.transform.position.x && 
            transform.position.y == player.transform.position.y - 1)
         {
             Debug.Log ("Player is above me");
             // player is directly above this tile
             pausePlayer = true;
         }

I'm thinking of using raycasts, but I have absolutely NO idea how to use those things at all. If you are using raycasts, it'd be great if you could write some code so I can understand. If you are writing code, it'd be preferred if it was in C# but I'm sure once I get the concept I can translate from UnityScript if needed.

Also note: Having a trigger collision will NOT work. I've tried, but it simply does not work with the gameplay.

Thanks in advance! YomanAwe

Comment
Add comment · Show 2
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 robertbu · Jul 16, 2014 at 01:25 AM 0
Share

Raycasting is a viable answer, but there may be simpler solutions. Is this a 2D or 3D game? Are you using an Orthographic or a Perspective camera? Do all tiles freeze the player or just some tiles?

avatar image aman_jha · Jul 16, 2014 at 02:21 AM 0
Share

It's technically a 3D game, but played as a 2D game. It is a perspective camera and only some tiles freeze the character

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by FirePlantGames · Jul 16, 2014 at 01:29 AM

make sure that the X position actually equals the EXACT same X position of player object, here is a ray cast code:

 var hit : RaycastHit;
     
     if (Physics.Raycast(transform.position,Vector3.down,hit))
             {
                 if (hit.gameObject == player.gameObject) //the players .gameObject is there because i'm not sure if you have it set to a transform, if it's a GameObject then you can be rid of it :)
                 {
                     Debug.Log ("Player is above me");
                     // player is directly above this tile
                     pausePlayer = true;
                 }
             }
 

that should work, hope I helped :D be sure to tell me how it goes!

Comment
Add comment · Show 11 · 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 robertbu · Jul 16, 2014 at 01:39 AM 0
Share

@memorymod - A couple of suggestions if the OP ever answers my comment above. First since since the player moves on the XY plane, I suspect the tiles will be either forward or back of the player, not above or below it. That means the raycast will use either Vector3.forward, or Vector3.back. Second, it is far more efficient for the player to raycast against the tiles and detect a property of the tiles and do the freeze rather than have all the tiles doing the raycast and having to inform the player he should freeze.

avatar image aman_jha · Jul 16, 2014 at 06:36 PM 0
Share

@robertbu what should I do then? Since all the tiles the player interacts with are below the player, what should I do so the player canact based on the tile that is below it?

avatar image robertbu · Jul 16, 2014 at 06:47 PM 0
Share

Are you using 2D or 3D colliders on your objects? Is 'below' lower on the 'Y' axis or is it behind as in further out the 'Z' axis?

avatar image aman_jha · Jul 17, 2014 at 01:38 AM 0
Share

3d colliders. everything in my game is 3d. below is lower on the y axis. the z axis is always constant for everything in the game. The player has a box collider and the width of the box collider is exactly the same width of the pause tile. shouldn't be hard but I have NO idea how to do it

avatar image aman_jha · Jul 17, 2014 at 01:39 AM 0
Share

@robertbu

Show more comments

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

24 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 avatar image avatar image

Related Questions

RayCast relative to camera stays in 1 place. 2 Answers

How I can update prefab properties using c# 1 Answer

Phyics.Raycast alternatively returns True/False with static input 2 Answers

Multiple Cars not working 1 Answer

Place Gameobject within a specific tranform value 3d 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