Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Rokiou · Jul 06, 2015 at 10:39 AM · scripting problemmovementraycastpacman

Raycast Help

This is sample of my Pac-Man movement code with a shell for the raycast. Every time I try to make some type of raycast restriction he ends up being stuck at the end of the first lane.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour 
 {
     public Rigidbody2D rb2D;
     public float speed = 0.3f;
 
     private Vector2 velocity;
     private Animator anim;
     private Vector2 newPos; 
 
     void Start() 
     {
         newPos = transform.position;
         rb2D = GetComponent<Rigidbody2D>();
         anim = GetComponent <Animator> ();
         velocity = Vector2.left;
     }
     void FixedUpdate() 
     {
         rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
 
         if (Input.GetAxisRaw ("Vertical") == (1)) {
             if(valid(Vector2.up))
             { 
                 velocity = Vector2.up;
                 anim.SetFloat ("dirY", 1);
                 anim.SetFloat ("dirX", 0);
                 rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
             }
         }
 
         if (Input.GetAxisRaw ("Horizontal") == (-1)) {
             if(valid(Vector2.left))
             { 
                 velocity = Vector2.left;
                 anim.SetFloat ("dirX", -1);
                 anim.SetFloat ("dirY", 0);
                 rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
             }
         }
 
         if (Input.GetAxisRaw ("Vertical") == (-1)) {
             if(valid(Vector2.down))
             { 
                 velocity = Vector2.down;
                 anim.SetFloat ("dirY", -1);
                 anim.SetFloat ("dirX", 0);
                 rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
             }
         }
 
         if (Input.GetAxisRaw ("Horizontal") == (1)) {
             if(valid(Vector2.right))
             { 
                 velocity = Vector2.right;
                 anim.SetFloat ("dirX", 1);
                 anim.SetFloat ("dirY", 0);
                 rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
             }
         }
     }
 
     bool valid (Vector2 velocity) 
     {
     
     }
 }

This is a photo of where he gets stuck. My colliders are lined up perfectly, and without the raycast he can move around the map perfectly.

alt text

I would appreciate some pseudo code, but not the actual code to make it work.

pac-man-stuck.png (21.1 kB)
Comment
Add comment · Show 1
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 Rokiou · Jul 19, 2015 at 06:44 AM 0
Share

I put a line in the script to log the tags the raycast gets to the console. When Pac-$$anonymous$$an gets to the spot in the picture above he can not turn up or down. The tag detected is a wall, which explains why he can't move, but there is no wall in his way, just pac-dots. I also observed something else weird. At times the console logs the tag its hitting as player, but since Pac-$$anonymous$$an is less than 1 unit and so is the collider why does it ever hit him. alt text

pac-man-raycast-log.png (125.3 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LaneFox · Jul 06, 2015 at 11:40 AM

Are you using Physics*2D*.Raycast() ?

Small side note.. GetAxisRaw should return 0 or 1 but in dealing with floats its always good practice to just go ahead and assume you'll never get an equal number due to floating point precision. I would set this up with <= / >= operators, but I'm OCD. :P

 Physics2D.Raycast(stuff)
 
 if (hit) return false;
 else return true;
Comment
Add comment · Show 5 · 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 Rokiou · Jul 06, 2015 at 11:50 AM 0
Share

GetAxisRaw returns a value of -1 or 1 exactly because it only returns whole numbers. I've tried the script above checking whether the ray collides with a collider tagged "wall" but that caused him to become stuck as mentioned before.

avatar image LaneFox · Jul 06, 2015 at 11:57 AM 0
Share

Right, and that should work fine. Can you post your raycast code?

avatar image Rokiou · Jul 06, 2015 at 12:09 PM 0
Share

I'll try to type this using my phone. I'm not at my PC right now but this code is similar to what i was trying to use. bool valid (Vector2 velocity) { RaycastHit2D hit = Physics2D.Raycast (newPos, velocity, 1); return (hit.collider.gameObject.tag != "wall"); }

avatar image LaneFox · Jul 06, 2015 at 12:20 PM 0
Share

It could be getting hung up on cases where there is no hit collider, add a check to narrow it down.

     private bool valid(Vector2 inputVector)
     {
         RaycastHit2D hit = Physics2D.Raycast(newPos, inputVector, 1);
         if (hit.collider != null)
         {
             return (hit.collider.gameObject.tag != "wall");
         }
 
         return true;
     }

After that if it still fails, are you certain that all of the walls are tagged "wall" and that there is adequate space for the character to fit between the colliders?

avatar image Rokiou · Jul 06, 2015 at 09:58 PM 0
Share

Sadly the script above did not work either. Pac-$$anonymous$$an still ends up stuck at the end of the first lane. As mentioned before I know that Pac-$$anonymous$$an can fit through all lanes because I can collect all the Pac-Dots without the raycast script. I have not mentioned something that might be an issue and thats that the walls are part of one sprite with multiple Box colliders, but im positive they are tagged "wall".

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

2 People are following this question.

avatar image avatar image

Related Questions

My Raycast acts like it's hitting something when it isn't 0 Answers

How to move the main camera in Google Cardboard? 0 Answers

Problem with shooting an object in 2D 1 Answer

Spawn script on mesh crashes Unity 0 Answers

Please Check the code and tell me why this isn't working?! 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