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 unity_X3UO0vbpCLAaIA · Jun 01, 2018 at 06:25 AM · collisionraycasting2d-physics

How to generate a raycast on keydown and store what it hits as a variable

Hello and disclaimer: I am a novice.
Some basic information: my project is a 2D strategy game mimicking Shining Force 2. Movement is based on a grid, and the player and all NPCs occupy 1 grid space (32x32 tile). My goal is to use the WASD keys to generate a short raycast in the direction pressed (eg press up, ray goes up) and use the collision information to do a number of things:
1. Given that player movement is accomplished with 1 unit X/Y transforms, the collision will prevent further movement in the direction collided with. This simulates grid movement. 2. Use collisions to set NPC, door, treasure box, etc scripts to an active state, mimicking a context sensitive button. eg, if collision with NPC A is true, the player will press a button to cause the diologue of NPC A to appear, or if collison with treasure box A is true, the player will press a button to open the box.
Is this feasible? So far I have this:

 Vector3 pos;
 public float speed = 2.0f;
 void Start()
 {
     pos = transform.position; // Take the current position
     anim = GetComponent<Animator>();

 }
 void FixedUpdate()
 {
     //====RayCasts====//
     RaycastHit2D hitup = Physics2D.Raycast(transform.position, Vector2.up, 1);
     RaycastHit2D hitdown = Physics2D.Raycast(transform.position, Vector2.down, 1);
     RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, 1);
     RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, 1);
     //==Inputs==//
     if (Input.GetKey(KeyCode.A) && transform.position == pos && hitleft.collider == null)
     {           //(-1,0)
         pos += Vector3.left * 1;// Add -1 to pos.x          
     }
     if (Input.GetKey(KeyCode.D) && transform.position == pos && hitright.collider == null)
     {           //(1,0)
         pos += Vector3.right * 1;// Add 1 to pos.x
     }
     if (Input.GetKey(KeyCode.W) && transform.position == pos && hitup.collider == null)
     {           //(0,1)
         pos += Vector3.up * 1; // Add 1 to pos.y
     }
     if (Input.GetKey(KeyCode.S) && transform.position == pos && hitdown.collider == null)
     {           //(0,-1)
         pos += Vector3.down * 1;// Add -1 to pos.y
     }
     //The Current Position = Move To (the current position to the new position by the speed * Time.DeltaTime)
     transform.position = Vector3.MoveTowards(transform.position, pos, speed);    // Move there  

This works very well to mimick the type of movement the 16 bit game had, it's basically perfect. However, I want the raycasts to only occur upon pressing one of the WASD keys and only in that direction, until another WASD key is pressed, while keeping the movement the same. How can I accomplish this?

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 Positive7 · Jun 01, 2018 at 06:32 AM 0
Share
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) && transform.position == pos)
         {
             RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, 1);
             if (hitleft.collider == null) pos += Vector3.left * 1;
         }

?

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

195 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

Related Questions

OnCollisionEnter2D is called after object was shifted 1 Answer

Raycast not always detecting floor 1 Answer

Layer Collision 0 Answers

How do I properly do a 2D raycast to avoid problem with pause button? 0 Answers

Player teleports in a random direction 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