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?
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
Your answer
Follow this Question
Related Questions
Manual RotateAround 0 Answers
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