- Home /
How do i rotate an object around an arbitrary axis in 3D?
I am implementing a system which enables the player to hold a GameObject in the air in a given place, relative to the player character itself. This is achieved trough a script i attach to the held object, which when picked up pulls the object towards a given point(Transform). The script also makes sure that the held object (on the 'y' axis) is facing the same direction as it's target (and the main camera). So far the script works beautifully. But i also want to make tha player be able to rotate the object using the scrollwheel.
~When the shift key is not pressed the scrollwheel rotates the object around the world 'y axis'.
~When the shift key is pressed the object should be rotated around an imaginary axis perpendicular to the target's (also the player's) facing direction excluding the 'y axis'. This is the part i stuggle with. Please help!
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Storable))]
[RequireComponent(typeof(Rigidbody))]
public class Holdable_v2 : MonoBehaviour
{
[SerializeField] private float minVelocity;
[SerializeField] private float targetRadius;
[SerializeField] private float speed;
[SerializeField] private float rotationSpeed = 1;
[HideInInspector] public bool isHeld;
private Transform target;
private Vector3 startRotation;
private Vector3 controlledRotation;
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
rb.interpolation = RigidbodyInterpolation.Interpolate;
gameObject.tag = "Holdable";
}
private void FixedUpdate()
{
if (isHeld)
{
GravitateTowards(target);
Rotate();
}
}
private void Update()
{
GetRotationInput();
}
private void GravitateTowards(Transform target)
{
Vector3 velocity = (target.position - transform.position) * speed;
if (velocity.magnitude < minVelocity) //clamp velocity
{
velocity = velocity.normalized * minVelocity;
}
if ((target.position - transform.position).magnitude > targetRadius)
{
rb.velocity = velocity;
}
else //stop
{
rb.velocity = new Vector3();
}
}
private void Rotate()
{
//rotation correction
Vector3 rotation = Vector3.up * target.rotation.eulerAngles.y;
rotation.x = transform.rotation.x;
rotation.z = transform.rotation.z;
transform.rotation = Quaternion.Euler(startRotation + rotation + controlledRotation);
}
private void GetRotationInput()
{
//get input for manual rotation
if (isHeld)
{
float input = Input.GetAxis("Mouse ScrollWheel");
if (Input.GetKey(KeyCode.LeftShift))
{
//
//The code i can't figure out goes here
//
}
else
{
controlledRotation += Vector3.up * input * rotationSpeed;
}
}
}
public void LinkTo(Transform target)
{
this.target = target;
isHeld = true;
rb.useGravity = false;
rb.freezeRotation = true;
Vector3 rotation = Vector3.up * target.rotation.eulerAngles.y;
startRotation = transform.rotation.eulerAngles;
startRotation -= rotation;
Storable storable = GetComponent<Storable>();
storable.HideIcon();
storable.isMarkable = false;
}
public void Delink()
{
target = null;
isHeld = false;
controlledRotation = new Vector3();
rb.useGravity = true;
rb.freezeRotation = false;
GetComponent<Storable>().isMarkable = true;
}
}