• 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 Cellery · May 14, 2020 at 04:26 PM · rigidbody2drotatearoundsciptingrotatearoundpivotmousex

Is there a RotateAround functionality for Rigidbody 2D's?

Hello,


I've been trying to make a top-down 2d game which involves the weapon swinging around the player based on the momentum of the mouse x input, I was previously transforming the weapon to pivot around the player based on the RotateAround function but it wasn't reacting to incoming projectile physics. I have since switched transforming to rigidbody.MoveRotation() but I cannot work out how to rotate around a specific pivot when using this form of rotation.


Here is the code in question, currently it does not rotate around the player but does rotate in place with mouse x input:

 using UnityEngine;
 using System.Collections;
  
 public class OrbitPlayer : MonoBehaviour {
  
     public Transform target;
     private Rigidbody2D myRigidbody;
     public float orbitDistance = 10.0f;
     private Vector3 relativeDistance = Vector3.zero;
     private float mouseXspeed;
     private Vector3 targetPosition;
 
     // Use this for initialization
     void Start () {
         if(target != null) 
         {
             relativeDistance = (transform.position - target.position).normalized * orbitDistance;
         }
         myRigidbody = GetComponent<Rigidbody2D>();
     }
 
     void Orbit(float mouseXspeed, Vector3 targetPosition)
     {
         if(target != null)
         {
             Debug.Log(targetPosition);
             // Moves with the target
             myRigidbody.MovePosition(targetPosition);
             // Rotates based on Mouse X input
             myRigidbody.MoveRotation(mouseXspeed);
         }
     }
     
     void Update () {
         
         targetPosition = target.position + relativeDistance;
 
         // Gets the movement of the mouse in the x axis
         float mouseXaxis = Input.GetAxis("Mouse X");
         // Keeps knowledge of the axis and adds accordingly to movement
         mouseXspeed += mouseXaxis;
     
     }
     
     void FixedUpdate() {
         Orbit(mouseXspeed, targetPosition);
     }
 }


Is there a way to use RotateAround for a rigidbody or is there an equivalent?

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Cellery · May 15, 2020 at 06:37 AM

I've instead managed to use the parametric equation to work out x and y coordinates to make the weapon move to specific points in a circle around the player with the radius of 1.


 using UnityEngine;
 using System.Collections;
  
 public class OrbitPlayer : MonoBehaviour {
  
     public Transform target;
     private Rigidbody2D myRigidbody;
     private Vector3 relativeDistance = Vector3.zero;
     private float mouseXspeed;
     private float mouseXaxis;
     private Vector3 targetPosition;
     private Vector3 weaponPos;
 
     // Use this for initialization
     void Start () {
         myRigidbody = GetComponent<Rigidbody2D>();
         mouseXspeed = 0;
     }
  
     void Orbit(float mouseXspeed, Vector3 weaponPos)
     {
         if(target != null)
         {
             targetPosition = target.position;
             // Moves with the target
             myRigidbody.MovePosition(targetPosition + weaponPos);
             // Rotates based on Mouse X input
             myRigidbody.MoveRotation(mouseXspeed);
             
         }
     }
 
     void Update () {
         
         // Gets the movement of the mouse in the x axis
         mouseXaxis = Input.GetAxis("Mouse X");
         // Keeps knowledge of the axis and adds accordingly to movement
         
         mouseXspeed += mouseXaxis;
 
         weaponPos.x = Mathf.Cos(mouseXspeed);
         weaponPos.y = Mathf.Sin(mouseXspeed);
         weaponPos.z = 0;
 
         Debug.Log("mouse X Speed: " + mouseXspeed);
 
         print("mouseXspeed: "+ mouseXspeed+ " weaponPos: " + weaponPos);
     
     }
      
     void FixedUpdate() {
         Orbit(mouseXspeed*(180/Mathf.PI), weaponPos);
     }
 }


This does initialize the weapon position to be at rotation 0 with the coordinates directly 1 unit right of the target (player) as cos(0)=1

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

204 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

Related Questions

Deformation of child object when rotating around a rotatable parent object 0 Answers

Rotate player around enemy (Lock system) 1 Answer

What the angle argument means in the Transform.RotateAround function? 1 Answer

How do I rotate an object around around another, following mouse position and facing it? 0 Answers

Limit rotation and return the initial angle of an object 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