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 FatBOY2 · May 20, 2020 at 10:07 AM · raycastdistancemodulus

PacMan 3D Clone Movement

I am trying to make a PacMan like game, where the player has to finish the classic version of PacMan first before he can "evolve" and learn new abilities. For the first level I try to be as close to the original version as possible (although in 3D). The part where I am having problems now is to script the behavior that if PacMan moves between two walls and there is no way for him to go up or down, but the player presses the up/down key that PacMan does not stop (or rotates to the given axis) but moves to the next valid point and THEN moves in the direction the player has put in.

public class PacManMovement : MonoBehaviour { public float speed = 4.0f;

 private Vector3 direction = Vector3.zero;


 // Update is called once per frame
 void Update()
 {
     CheckInput();

     Move();

     UpdateOrientation();
 }

 void CheckInput()
 {
     if (CanTurn(direction))
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             direction = Vector3.left;
         }
         else if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             direction = Vector3.right;
         }
         else if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             direction = Vector3.forward;
         }
         else if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             direction = Vector3.back;
         }
     }
     
 }

 void Move()
 {
     if (Valid(direction))
     {
         transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime;
     }
 }

 void UpdateOrientation ()
 {
     if (Valid(direction))
     {
         if (direction == Vector3.left)
         {
             //transform.localScale = new Vector3(-100, 100, 100);
             transform.localRotation = Quaternion.Euler(-90, 0, 90);

         }
         else if (direction == Vector3.right)
         {
             //transform.localScale = new Vector3(100, 100, 100);
             transform.localRotation = Quaternion.Euler(-90, 0, -90);

         }
         else if (direction == Vector3.back)
         {
             //transform.localScale = new Vector3(100, 100, 100);
             transform.localRotation = Quaternion.Euler(-90, 0, 0);
         }
         else if (direction == Vector3.forward)
         {
             //transform.localScale = new Vector3(100, 100, 100);
             transform.localRotation = Quaternion.Euler(-90, 0, 180);
         }
     }
 }

 private bool Valid(Vector3 direction)
 {
     // Cast Line from 'next to Pac-Man' to 'Pac-Man'     
     Vector3 position = transform.position;
     RaycastHit hit;
     Ray ray = new Ray(position, direction);

     if (Physics.Raycast(ray, out hit, 1.0f))
     {
         if (hit.collider != null && hit.collider.name != "pacman")
         {
             Debug.Log(hit.collider.name);
             return false;
         }
     }
     return true;
 }

 private bool CanTurn(Vector3 direction)
 {
     Vector3 position = transform.position;
     RaycastHit longHit;
     Ray ray = new Ray(position, direction);
     if (Physics.Raycast(ray, out longHit))
     {
         if (longHit.distance > 1.0f && this.transform.position.x % 2 == 0 && this.transform.position.z % 2 == 0)
         {
             Debug.Log(longHit.collider.name);
             return true;
         }
     }
     return false;
 }

}

Now the CanTurn class is the part that does not work as I intended. Maybe Im thinking wrong here, I am kind of new to scripting. But I thought making PacMan check if the position is able to be divided with 0 remainder it would work, but I am not sure where I am going wrong with this. Any help is appreciated! Also sorry for the weird formatting

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

251 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 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

Calculating distance is not working 1 Answer

How to use RaycastHit ? 0 Answers

Checking for Raycast distances not working as expected. 0 Answers

Need to place an object at a certain angle form the camera and at a certaine distance 0 Answers

Scaling Crosshair 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