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 /
This question was closed Dec 17, 2018 at 04:02 PM by human3562 for the following reason:

figured it out myself

avatar image
0
Question by human3562 · Dec 15, 2018 at 04:19 PM · 2dspritestopdownangles

2D top-down multiple sprite rotation

So i am trying to make a character movement system that looks cool and i've run into a major problem. I have this 2D top-down character that consists of three sprites: head, body and legs. The head rotates towards the mouse using the atan2 function, the legs rotate if the angle between the head and legs is greater than 90 degrees, and the body is just half the angle between the head and legs. The problem is that the atan2 function jumps between -PI and PI, and instead of the legs continuing to follow the head, it acts as if the head got rotated the other way really quickly, which is not at all what i want. Please help, my brain is melting!alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerController : MonoBehaviour {
 
     float a;
     float aH;
     float aL;
     float aB;
     float t = 10f;
     float legspace = Mathf.PI / 2;
 
     public GameObject head;
     public GameObject body;
     public GameObject legs;
 
     
     void Update () {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         a = Mathf.Atan2(mousePos.y,mousePos.x);
         if (a < 0) a += Mathf.PI * 2;
         Quaternion a1 = Quaternion.Euler(0, 0, aH);
         Quaternion a2 = Quaternion.Euler(0, 0, a);
         Quaternion aHq = Quaternion.Slerp(a1, a2, t * Time.deltaTime);
         aH = aHq.eulerAngles.z;
         if ((aH - aL) > legspace) aL = aH - legspace;
         else if ((aH - aL) < -legspace) aL = aH + legspace;
         aB = ((aH - aL)) / -2 + aH;
 
         legs.transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * aL);
         body.transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * aB);
         head.transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * aH);
     }
 }
 


untitled-1.gif (318.9 kB)
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

1 Reply

  • Sort: 
avatar image
0

Answer by Wolfride · Dec 16, 2018 at 03:28 AM

I mean... Can't you just parent all 3 pieces to an empty game object and just rotate that? Or you don't want them all to move at the same time and your problem is the head is moving too fast while everything else is fine? And if you just didn't have the head rotate as far, it would look really good as it.

Comment
Add comment · Show 1 · 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 human3562 · Dec 16, 2018 at 09:12 AM 0
Share

The thing is that i want the player to continue turning one way, without that sudden jump on the right side. And i dont wanna limit the head movement either, i want him to be able to turn 360 freely without the sudden jump

Follow this Question

Answers Answers and Comments

193 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

Related Questions

I have an animaiton problem about sprite loading. 0 Answers

Vector art Player 0 Answers

Crop/Mask a Sprite on runtime 0 Answers

Render sprite on sprite ignoring alpha? 1 Answer

Why is my sprite not the original resolution? 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