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 jackblack2215 · Jan 12, 2017 at 12:10 PM · collisionbeginnerboolean

Why is this code not working?

It was working fine before I tried to add an animation. The animation didn't work so I just decided to figure it out later, I deleted everything that involved the animation from the script... then it stopped working! My player (a cube) goes straight through the walls, and when it touches the walls its suppose to stop being able to move in that direction. `using UnityEngine; using System.Collections;

public class Move : MonoBehaviour { public bool canUp = true; public bool canDown = true; public bool canLeft = true; public bool canRight = true;

 void Update()
 {
     if (canUp == true)
     {
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             transform.Translate(1f, 0f, 0f);
         }
     }
     if (canDown == true)
     {
         if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             transform.Translate(-1f, 0f, 0f);
         }
     }
     if (canLeft == true)
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             transform.Translate(0f, 0f, 1f);
         }
     }
     if (canRight == true)
     {
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             transform.Translate(0f, 0f, -1f);
         }
     }
 }
 void OnCollisionEnter(Collision wall)
 {
     if (wall.gameObject.tag == "TopWall")
     {
         canUp = false;
     }
     if (wall.gameObject.tag == "BottomWall")
     {
         canDown = false;
     }
     if (wall.gameObject.tag == "LeftWall")
     {
         canLeft = false;
     }
     if (wall.gameObject.tag == "RightWall")
     {
         canRight = false;
     }
 }
 void OnCollisionExit(Collision wall)
 {
     if (wall.gameObject.tag == "TopWall")
     {
         canUp = true;
     }
     if (wall.gameObject.tag == "BottomWall")
     {
         canDown = true;
     }
     if (wall.gameObject.tag == "LeftWall")
     {
         canLeft = true;
     }
     if (wall.gameObject.tag == "RightWall")
     {
         canRight = true;
     }
 }

}

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 jackblack2215 · Jan 12, 2017 at 06:46 AM 0
Share

Never$$anonymous$$d! I figured it out! $$anonymous$$y player needed a rigidbody on it so that it would collide with the walls, but if there is an easier was to do this I would love to know.

avatar image Spider_newgent · Jan 12, 2017 at 01:48 PM 0
Share

Hi.

Glad you've sorted it out.

Take a look at GetAxis at this link: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

I'd recommend searching for some tutorials on it if you aren't sure how to implement it in your game.

Good luck.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision Detection between two different Prefabs doesnt work 1 Answer

Collision occurs but nothing happens? 0 Answers

Collision between two GameObjects causing an increase in velocity 0 Answers

Hovering PlayerObject interacting with World Space in a confusing manner 0 Answers

Make the player go from a hook to another hook 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