Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by error13660 · Aug 04, 2021 at 07:36 PM · rotationrigidbodytransform3dquaternion

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;
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

246 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Object slope rotation issue 0 Answers

Problem with rigidbodies and rotation with quaternion 1 Answer

Player character X rotation going haywire on game start 1 Answer

How to ROTATE an object without slowing the ends (lerp) 3 Answers

need help regarding rotating arm 0 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