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 CoderCole · Apr 08 at 12:42 AM · rotatecamera rotaterotatearoundcar gamerotatearoundpivot

RotateAround() using local rotation axes

Hello, I am making a car customizer where the player can navigate around the car (like in blender with middle mouse button), but I am running into a problem. I am using RotateAround(), like so: camTransform.RotateAround(Vector3.zero, transform.up, Input.GetAxis("Mouse X") 3); camTransform.RotateAround(Vector3.zero, transform.forward, Input.GetAxis("Mouse Y") 3); camTransform.rotation = Quaternion.Euler(camTransform.eulerAngles.x, camTransform.eulerAngles.y, 0); My problem is that the camera rotates differently depending on its location. For example, if you look at the car from the side and drag vertically, you rotate around the car's z axis (like intended). BUT, if you are looking at the car dead on and drag vertically, you still rotate around the car's z axis (not intended), and your camera just ends up doing a small circle midair. I thought that using transform. up and transform.forward would use the cameras local axes, and I wouldn't have this problem. But I guess not. All help is appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by CoderCole · Apr 08 at 05:22 AM

For anyone who potentially had the same problem as me - I found a solution. I added a GameObject called PivotPoint where I wanted the camera to rotate around, (0, 0, 0) in my case, then I made the pivot point always face towards the camera on the y axis, like so: "PivotPoint.LookAt(new Vector3(camTransform.position.x, 0, camTransform.position.z), PivotPoint.up);" Then I simply made the RotateAround() lines rotate around the PivotPoint's local axes, like this: "camTransform.RotateAround(Vector3.zero, PivotPoint.up, Input.GetAxis("Mouse X") LookSpeed); camTransform.RotateAround(Vector3.zero, PivotPoint.right, Input.GetAxis("Mouse Y") LookSpeed); camTransform.rotation = Quaternion.Euler(camTransform.eulerAngles.x, camTransform.eulerAngles.y, 0);". Hope this helped :)

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

Answer by CoderCole · Apr 08 at 12:45 AM

Here is the entire script if that helps:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;

public class CarBuilder : MonoBehaviour { public float LookSpeed = 3; public float ScrollSpeed = 10; public float MinFOV = 30; public float MaxFOV = 120; private Transform camTransform; private Camera cam; private int UILayer; private bool Clicked;

 private void Start()
 {
     UILayer = LayerMask.NameToLayer("UI");
     cam = Camera.main;
     camTransform = cam.transform;

     camTransform.LookAt(Vector3.zero);
 }

 private void Update()
 {
     bool OverUI = IsPointerOverUIElement(GetEventSystemRaycastResults());

     if (Input.GetMouseButtonDown(0) && !OverUI)
     {
         Clicked = true;
     }

     if (Input.GetMouseButton(0) && Clicked && !OverUI)
     {
         if (Cursor.lockState == CursorLockMode.None) Cursor.lockState = CursorLockMode.Locked;
         camTransform.RotateAround(Vector3.zero, transform.up, Input.GetAxis("Mouse X") * LookSpeed);
         camTransform.RotateAround(Vector3.zero, transform.forward, Input.GetAxis("Mouse Y") * LookSpeed);
         camTransform.rotation = Quaternion.Euler(camTransform.eulerAngles.x, camTransform.eulerAngles.y, 0);
     }
     else
     {
         Clicked = false;
         if (Cursor.lockState == CursorLockMode.Locked) Cursor.lockState = CursorLockMode.None;
     }

     if (Input.GetAxis("Mouse ScrollWheel") > 0f && cam.fieldOfView > MinFOV && !OverUI)
     {
         cam.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;
     }
     else if (Input.GetAxis("Mouse ScrollWheel") < 0f && cam.fieldOfView < MaxFOV && !OverUI)
     {
         cam.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;
     }
 }

 private bool IsPointerOverUIElement(List<RaycastResult> eventSystemRaysastResults)
 {
     for (int index = 0; index < eventSystemRaysastResults.Count; index++)
     {
         RaycastResult curRaysastResult = eventSystemRaysastResults[index];
         if (curRaysastResult.gameObject.layer == UILayer)
             return true;
     }
     return false;
 }

 static List<RaycastResult> GetEventSystemRaycastResults()
 {
     PointerEventData eventData = new PointerEventData(EventSystem.current);
     eventData.position = Input.mousePosition;
     List<RaycastResult> raysastResults = new List<RaycastResult>();
     EventSystem.current.RaycastAll(eventData, raysastResults);
     return raysastResults;
 }

}

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.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

How can I make the player rotate with the camera so my movement keys don't get messed up because when I do rotate the camera the move keys go in different directions. 0 Answers

Based on touch rotate camera object 0 Answers

RotateAround() faster than rotate 1 Answer

How to unlock cursor while moving the character? 0 Answers

is it possible to have your camerarotation be *-1 of you player rotation. 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