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 simplicitydown · Dec 11, 2015 at 05:20 PM · c#2draycastingstrafing

Strafing diagonally using get.axis in Unity 5?

Hi all! I know there is information on freezing axis around but 'constraints' have been deprecated and the documentation on 'freeze' is a one-liner. Also, my character is moving using get.axis which is powered with just a horizontal and vertical axis. My code allows movement to be recognized and for melee attacks to be carried out in 8 directions, but it doesn't seem to have brought me any closer to the goal of strafing. Here is my relevant code:

 private bool moveRight = false, moveLeft = false, moveUp = false, moveDown = false;
 
     void Start ()
     {
         playerRigid = GetComponent<Rigidbody2D> ();
     }
 
     void Update ()
     {
 
         float inputX = Input.GetAxis ("Horizontal");
         float inputY = Input.GetAxis ("Vertical");
 
         movement = new Vector2 (
             speed.x * inputX,
             speed.y * inputY);
 
 
          if (movement.x > 0f) {
             moveRight = true; moveLeft = false; moveUp = false; moveDown = false;
             Debug.DrawLine (lineStart.position, rightLineEnd.position, Color.green);
         } 
 
         if (movement.x < 0f) {
             moveRight = false; moveLeft = true; moveUp = false; moveDown = false;
             Debug.DrawLine (lineStart.position, leftLineEnd.position, Color.green);
         } 
             
         if (movement.y < 0f) {
             moveRight = false; moveLeft = false; moveUp = false; moveDown = true;
             Debug.DrawLine (lineStart.position, downLineEnd.position, Color.green);
 
         } if (movement.y > 0f) {
             moveRight = false; moveLeft = false; moveUp = true; moveDown = false;
             Debug.DrawLine (lineStart.position, upLineEnd.position, Color.green);
         
         }
 
 
         if (movement.x > 0f  && movement.y > 0f) {
             moveRight = true; moveLeft = false; moveUp = true; moveDown = false;
             Debug.DrawLine (lineStart.position, upRightLineEnd.position, Color.green);
         } 
 
         if (movement.x < 0f  && movement.y > 0f) {
             moveRight = false; moveLeft = true; moveUp = true; moveDown = false;
             Debug.DrawLine (lineStart.position, upLeftLineEnd.position, Color.green);
         } 
 
         if (movement.x > 0f  && movement.y < 0f) {
             moveRight = true; moveLeft = false; moveUp = false; moveDown = true;
             Debug.DrawLine (lineStart.position, downRightLineEnd.position, Color.green);
         } 
 
         if (movement.x < 0f  && movement.y < 0f) {
             moveRight = false; moveLeft = true; moveUp = false; moveDown = true;
             Debug.DrawLine (lineStart.position, downLeftLineEnd.position, Color.green);
         } 
 
         Raycasting ();
 
     }
         
     void FixedUpdate ()
     {
         playerRigid.velocity = movement;
 
     }
 
     void Raycasting()
         {
                 
                 
         if (Physics2D.Linecast (lineStart.position, rightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveRight == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, rightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
                 }
 
         if (Physics2D.Linecast (lineStart.position, leftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveLeft == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, leftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
                 }
         if (Physics2D.Linecast (lineStart.position, upLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, upLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
                 }
 
         if (Physics2D.Linecast (lineStart.position, downLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveDown == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, downLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
         }
 
         if (Physics2D.Linecast (lineStart.position, upRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true && moveRight == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, upRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
         }
 
         if (Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true && moveLeft == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
         }
 
         if (Physics2D.Linecast (lineStart.position, downRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveDown == true && moveRight == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, downRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
         }
 
         if (Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true && moveLeft == true) {
             whatIHit = Physics2D.Linecast (lineStart.position, downLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
         }
 
         if (Input.GetKeyDown (KeyCode.E) && mUpgrade1 == false) {
             whatIHit.collider.gameObject.GetComponent<EnemyHealth>().DamageE1();
         }
             
         if (Input.GetKeyDown (KeyCode.E) && mUpgrade1 == true) {
             whatIHit.collider.gameObject.GetComponent<EnemyHealth>().DamageE2();
         }
 
         }

Just a simple if statement (or a series of them, I only need a start) to take a key input to lock the axis of the direction the player is facing/attacking. This would really help, thanks so much!

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

51 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

Related Questions

Raycast on mouse position problem. how not to raycast to 0 1 Answer

Raycast with Quaternion directions (C#) 1 Answer

Moving left and right with one button in 2d Game 1 Answer

Camera Movement Question - How to follow player in Y axis only above certain threshold? 0 Answers

How to check if player is rapidly rotating the joystick? 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