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?