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 hippokros · Apr 06, 2014 at 01:49 PM · raycastingvector2box2d

Box2D empty gameObjects not getting expected results code is included

See screen for explanation: your the blue square and you press a key and you move down. if you hit the block you should stop.

However if you hit the top of the green block with your bottom you shouldn't be able to go any further toward the bottom

I created my own playercontrol sort of like what Unity training videos has with their CharacterController2D and i have empty gameobjects to the top left bottom and right of the box

and here is the provided code below

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMove : MonoBehaviour {
 
     public float startingSpeed = 5;
     public bool moveLeft { get; set; }
     public bool moveRight { get; set; }
     public bool moveUp { get; set; }
     public bool moveDown { get; set; }
     public bool dontMove { get; set; }
     public float Speed { get; set; }
     public bool isGroundedBottom = false;
     public bool isGroundedLeft = false;
 
     Transform TopCheck;
     Transform LeftCheck;
     Transform BottomCheck;
     Transform RightCheck;
     [SerializeField] LayerMask whatIsGround;
     float Radius = .2f;
 
     void FixedUpdate(){
         /*if (Physics2D.OverlapCircle (new Vector2(BottomCheck.position.x, BottomCheck.position.y), Radius, whatIsGround)) {
                         moveDown = false;
                 }
         if (Physics2D.OverlapCircle (new Vector2(LeftCheck.position.x, LeftCheck.position.y), Radius, whatIsGround)) {
                         moveLeft = false;
                 }
         if(Physics2D.OverlapCircle(new Vector2(RightCheck.position.x, RightCheck.position.y), Radius, whatIsGround)){
                         moveRight = false;
         }
         if(Physics2D.OverlapCircle(new Vector2(TopCheck.position.x, TopCheck.position.y), Radius, whatIsGround)){
                         moveUp = false;
         }*/
 
 
     }
 
     void Awake(){
         TopCheck = transform.Find ("TopCheck");
         LeftCheck = transform.Find ("LeftCheck");
         BottomCheck = transform.Find ("BottomCheck");
         RightCheck = transform.Find ("RightCheck");
     }
 
     // Use this for initialization
     void Start () {
         Speed = startingSpeed;
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         DontMove ();
         Debug.Log ("TEST HIT");
         Debug.Log ("Game Object Name" + other.name.ToString () + "Tag" + other.tag.ToString () + "Other.Gameobject" + other.gameObject.name.ToString () + "other gameObject Tag" + other.gameObject.tag.ToString());
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         Debug.Log ("Game Object Name" + other.name.ToString () + "Tag" + other.tag.ToString () + "Other.Gameobject" + other.gameObject.name.ToString () + "other gameObject Tag" + other.gameObject.tag.ToString());
     }
     // Update is called once per frame
     void Update () {
 
                 if (Input.GetKey (KeyCode.Space)) {
                         dontMove = true;
                         moveLeft = false;
                         moveRight = false;
                         moveUp = false;
                         moveDown = false;
                 }
 
                 if ((Input.GetAxis ("Horizontal") < -.1) || Input.GetKey (KeyCode.LeftArrow)) {
                         MoveLeft ();
                 }
 
                 if ((Input.GetAxis ("Horizontal") > .1) || Input.GetKey (KeyCode.RightArrow)) {
                         MoveRight ();
                 }
 
                 if ((Input.GetAxis ("Vertical") > .1) || Input.GetKey (KeyCode.UpArrow)) {
                         MoveUp ();
                 }
 
                 if ((Input.GetAxis ("Vertical") < - .1) || Input.GetKey (KeyCode.DownArrow)) {
                         MoveDown ();
                 }
 
                 //move player according to which direction they can move
                 if (moveLeft) {
                         this.transform.Translate ((-1) * Speed * Time.deltaTime, 0f, 0f);
                 } else if (moveRight) {
                         this.transform.Translate (Speed * Time.deltaTime, 0f, 0f);
                 } else if (moveUp) {
                         this.transform.Translate (0f, Speed * Time.deltaTime, 0f);
                 } else if (moveDown) {
                         this.transform.Translate (0f, (-1) * Speed * Time.deltaTime, 0f);
                 } else if (dontMove) {
                         this.transform.Translate (0f, 0f, 0f);
                 }
                 //check to see if we go off the screen then reset
                 if (this.transform.position.x <= -10) {
                         StopIt ();
                         this.transform.position = new Vector3 (0f, 0f, 0f);
                 } else if (this.transform.position.x >= 200) {
                         StopIt ();
                         this.transform.position = new Vector3 (0f, 0f, 0f);
                 } else if (this.transform.position.y <= -10) {
                         StopIt ();
                         this.transform.position = new Vector3 (0f, 0f, 0f);
                 } else if (this.transform.position.y >= 200) {
                         StopIt ();
                         this.transform.position = new Vector3 (0f, 0f, 0f);
                 }
 
         //check collision on update
 
 
     }
 
     private void StopIt()
     {
         dontMove = true;
         moveUp = false;
         moveLeft = false;
         moveRight = false;
         moveDown = false;
     }
 
     private void MoveLeft()
     {
         moveLeft = true;
         moveRight = false;
         moveUp = false;
         moveDown = false;
     }
 
     private void MoveRight()
     {
         moveRight = true;
         moveLeft = false;
         moveDown = false;
         moveUp = false;
     }
 
     private void MoveDown()
     {
         moveDown = true;
         moveLeft = false;
         moveRight = false;
         moveUp = false;
     }
 
     private void MoveUp()
     {
         moveUp = true;
         moveLeft = false;
         moveRight = false;
         moveDown = true;
     }
 
     private void DontMove()
     {
         dontMove = true;
         moveDown = false;
         moveLeft = false;
         moveRight = false;
         moveUp = false;
     }
 
 }
     
 

alt text

I've commented out FixUpdate since i thought my empty gameobjects i'm trying to check collision with much like how the sample prefab 2d robot in the asset store checks for ground and ceiling i made sure the were nested under the player prefab and that and the players sprite was under layer, "Player" but whenever i added the FixUpdate it moved 5 pixels and would stop move 5 pixels and stop

What I am looking for in this example as i'm learning unity and i'm a noob is press a key move in direction until you collide bottom of your sprite to the top if you move down and you collide against top sprite you can't move down

I can't figure this out for some reason guess i'm just a dumb noob :(

PLEASE HELP!!!!

screenshot 2014-04-06 06.43.38.png (197.7 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

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

21 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

Related Questions

Converting direction to Vector2 2 Answers

Is there a more efficient way to pass two integers via "SendMessage" than using a Vector2 Struct? 1 Answer

Rotating A Vector2 Input Into A Vector3 1 Answer

Angle between two lines in 2D 3 Answers

Vector2 Lerp alternative? 2 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