• 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 AtainEndevor · Jul 11, 2017 at 03:22 AM · 2d rotation2d controller

2D Two Layer Player Controller

Hello,

I'm trying to make a 2D top down shooter. Hopefully in the future I hope to expand this a little more to incorporate some basic construction, but I digress. My focus right now is on the controller.

My idea was to have a two layer sprite, bottom layer being the legs, top layer being the torso and the arms. The top layer would have a controller script to follow the direction of the mouse. I have that here:

 void FixedUpdate()
 {
     Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
     transform.rotation = rot;
     transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
 }


Which that does a great job rotating the character wherever the mouse is.

What I'm working on now is the direction of the legs. I have several animations to include strafing and back stepping, the range I'm basing on will be:

"W" - Moving Forward "W+A" / "W+D" - Moving at a 45 degree angle "A" / "D" - Strafing left or right "S" - Back step

Instead of W being up on the screen, WASD should be based on where the mouse is.

I've looked around to several places and things don't really seem to be doing what I think they should:

 void FixedUpdate()
 {

     float inputF = Input.GetAxis("Vertical");
     float inputR = Input.GetAxis("Rotate");

  
     Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

     if (inputR == 0)
         transform.rotation = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
     //anim.SetBool("Moving", true);
     if (inputR > 0)
     {
         Vector3 rV = Quaternion.Euler(45,0,0) * mousePosition;
         transform.rotation = Quaternion.LookRotation(transform.position + rV, Vector3.forward);
     }
     if (inputR < 0)
     {
         Vector3 rV = Quaternion.Euler(45,0,0) * mousePosition;
         transform.rotation = Quaternion.LookRotation(transform.position - rV, Vector3.forward);
     }

     Debug.Log("MouseX = " + mousePosition.x);

     transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
 }


I'm pretty sure I'm over thinking this, and this isn't the only method I've tried. They all seem to yield the same results though, the feet will rotate with the mouse if nothing is pressed, but if you press something, the feet move one direction or the other just slightly, not 45 degrees.

EDIT

After doing a little more research, I found an approach that seems to be more on par with what I'm trying to achieve... I'm also trying to actually rotate about the right axis.

What I'm seeing though is the rotation seems to only be on one half. IE, if my mouse is north of the sprite, the feet rotate based on the mouse's location (granted not exactly the right way, but we're still taking baby steps). If I move the mouse below, the feet still bounce back and forth facing the top as if the mouse were still above the sprite.

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 AtainEndevor · Jul 13, 2017 at 02:36 PM 0
Share

If it helps any, I drew up a diagram below. Sprite comes in two layers. The diagram only shows the top layer, but rotates freely based on the mouse position. The bottom layer will show the feet moving based on WASD. W moves towards the mouse, while A/D are left/right based on the mouse position. In theory, the sprite will only move on 45 degree angles from the mouse. AW, W, and WD will run the forward animation, but need to rotate 45 degrees from the mouse location which is the part I'm having issues with. A and D will simply switch animations to strafing, so the second layer technically won't need to rotate, but simply follow the mouse like the upper half. Back stepping will follow a similar process as forward, just different animation.

alt text

I believe it's a simple question and I'm sure I'm just overthinking the math side of it.

What rotation or quaternion do I need to generation to rotate 45 degrees from the mouse position?

player-layout.png (72.5 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AtainEndevor · Jul 13, 2017 at 07:46 PM

So... Got it. Not a complete controller, but the framework is there at least. Turned out a lot more simple than I definitely thought originally. If anyone else can benefit, here's my new code:

 using UnityEngine;
 using System.Collections;

 public class PlayerFeetController : MonoBehaviour
 {

 Animator anim;
 private float rotateOffset = 0f;

 void Start()
 {
     //anim = GetComponent<Animator>();
 }

 void FixedUpdate()
 {

     float inputF = Input.GetAxis("Vertical");
     float inputR = Input.GetAxis("Rotate");


     Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

     //Standing Still
     if (inputR == 0 && inputF == 0)
         rotateOffset = 0;

     //Walking Forward
     if (inputR == 0 && inputF > 0)
     {
         rotateOffset = 0;
     }

     //Walking Angle Left
     if (inputR > 0 && inputF > 0)
     {
         rotateOffset = -45;
     }

     //Walking Angle Right
     if (inputR < 0 && inputF > 0)
     {
         rotateOffset = 45;
     }

     //Walking Left
     if (inputR > 0 && inputF == 0)
     {
         rotateOffset = 0;
         //set Animation
     }

     //Walking Right
     if (inputR < 0 && inputF == 0)
     {
         rotateOffset = 0;
         //set Animation
     }

     //Walking Backwards
     if (inputF < 0)
     {
         //set Animation
     }

     transform.rotation = Quaternion.AngleAxis(rotateOffset, Vector3.forward) * Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
     transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
 }
 }


So step one complete, I'm going to work on my animation controller now. After that is the procedurally 'infinite' tile terrain which I'm sure I'll have another post with a question or two on.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

109 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

Related Questions

How can I make player shoot in 2d with a joystick when we move the joystick out of its circuler area 0 Answers

strange rotation in 2d 0 Answers

Enemy's projectile is firing at the opposite direction when it's y-axis align with the player's 0 Answers

how do i make my sprite turn to face the direction that my cursor is moving? 0 Answers

How to fix spite character changing position when turning? 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