Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Inf1n1teZer0 · Mar 28, 2016 at 03:05 AM · c#raycasttransformbooleanhit.point

For my tile puzzle game, how can i have the player cube only step on a tile once and if they step on the tile a second time the game will be lost?

I currently have a tile play area where the player can move 1 unit in 4 directions, I have tried a lot to get this to work and so far no luck, I am using a Raycast to detect each tile as the player is on it, and i have the tile change to blue when it is stepped on, but if it is stepped on a second time i would like it to turn to red. I am aware of OnTriggerEnter and Exit but I feel like it would be more convenient to use a Raycast for this to happen.

Thanks in advance for any help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Veerababu.g · Mar 28, 2016 at 04:58 AM

public boo hasfirstJump = false;

void OnTriggerEnter(collider other){ if(other.compareTag("playerTag"){ if(hasfirstJump == false){ hasfirstJump = true; //return }else //write your code for level faild } }

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

Answer by Addie · Mar 28, 2016 at 05:34 AM

@Inf1n1teZer0 this might help

 using UnityEngine;
 using System.Collections;
 
 public class Tile : MonoBehaviour
 {
     public Player player;
 
     public bool steppedOn = false;
     public int steppedCount = 0;
 
     void Start ()
     {
         player = FindObjectOfType<Player>();
     }
 
     void Update ()
     {
         if(steppedCount == 1 && player.gameOver == false)
         {
             transform.GetComponent<Renderer>().material.color = Color.yellow;
         }
 
         else if(steppedCount == 2 && player.gameOver == false)
         {
             transform.GetComponent<Renderer>().material.color = Color.red;
 
             StartCoroutine(GameOver());
         }
     }
 
     private IEnumerator GameOver()
     {
         player.gameOver = true;
         Debug.Log("GAME OVER");
 
         while(player.gameOver == true)
         {
             foreach(Tile allTiles in player.allTiles)
             {
                 allTiles.GetComponent<Renderer>().material.color = Color.red;
 
                 yield return new WaitForSeconds(0.1f);
 
                 allTiles.GetComponent<Renderer>().material.color = Color.yellow;
             }
         }
     }
 }

place the top script on all the tile gameobjects and place the player script on your player gameobject

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public bool gameOver = false;
 
     private Tile hitTile;
     public Tile[] allTiles;
 
     void Start ()
     {
         allTiles = FindObjectsOfType<Tile>();
     }
 
     void Update ()
     {
         if(gameOver == false)
         {
             CheckTileRay();
             PlayerMovement();
         }
     }
 
     void CheckTileRay()
     {
         RaycastHit hit;
 
         if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f))
         {
             if (hit.collider)
             {
                 hitTile = hit.collider.GetComponent<Tile>();
 
                 if (hitTile.steppedOn == false)
                 {
                     hitTile.steppedCount += 1;
                     hitTile.steppedOn = true;
                 }
             }
         }
     }
 
     void PlayerMovement()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             hitTile.steppedOn = false;
             transform.position += new Vector3(-1, 0, 0);
         }
 
         if (Input.GetKeyDown(KeyCode.D))
         {
             hitTile.steppedOn = false;
             transform.position += new Vector3(1, 0, 0);
         }
 
         if (Input.GetKeyDown(KeyCode.W))
         {
             hitTile.steppedOn = false;
             transform.position += new Vector3(0, 0, 1);
         }
 
         if (Input.GetKeyDown(KeyCode.S))
         {
             hitTile.steppedOn = false;
             transform.position += new Vector3(0, 0, -1);
         }
     }
 }
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

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

Related Questions

how to make a objesc follow the mouse in the 3d world,how to make a object follow the mouse on the 3d world 0 Answers

Raycast only occurs on start 2 Answers

2D Raycast reflection only changes hit.normal 0 Answers

Raycast not changing value 0 Answers

Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 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