• 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
1
Question by anarhist1994 · Nov 05, 2017 at 03:59 PM · rotation axis

How to make an object rotate around the camera on Y axis

I have an object, cube which has the following lines of code:

 Vector3 toPos = target.position + (target.rotation * defaultDistance);
         toPos = new Vector3 (toPos.x, 0f ,toPos.z); //problem
         this.transform.LookAt( target ) ;
         Vector3 curPos = Vector3.Lerp (myTransform.position, toPos, distanceDamp * Time.deltaTime);
         myTransform.position = curPos;


The problem is: When I rotate the camera around the X-Axis, after some degrees (over 20), the cube object moves forward (into the camera's position) instead of continue on moving in a circular movement.

What I'm trying to achieve, simply is like a solar system that the cube follows the camera's rotation only on y axis.

Update #2:

  Vector3 toPos = target.position + target.rotation * new Vector3(defaultDistance.x, 0.0f, defaultDistance.z);
         float multiplier = Mathf.Sqrt(defaultDistance.x * defaultDistance.x + (defaultDistance.z) * (defaultDistance.z));
         //multiplier /= Mathf.Sqrt(toPos.x * toPos.x + toPos.z * toPos.z); 
         toPos.x *= -multiplier;
         //toPos.y = target.position.y + defaultDistance.y;
         toPos.z *= -multiplier ;
         toPos.z -= 70;

     Vector3 curPos = Vector3.Lerp(myT.position, toPos, distanceDamp * Time.deltaTime);
     myT.position = curPos;



I almost got it right, the cube still isn't centered on the center. Can anyone help me figure out how to fix that?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FabDynamic · Nov 05, 2017 at 05:26 PM

Updated Answer:
Ok using your new code I see most of what you are talking about (although in my case the cube jumps to BFE instead of the camera location).

For simplicity's sake I am focusing on getting the rotations right. Try doing the LookAt towards a vector3 representation of the camera but on the xz plane like this:

 public Transform cameraT;
 public Transform myT;

 void LateUpdate () { 
     Vector3 clampedTargetPos = new Vector3 (cameraT.position.x, myT.position.y, cameraT.position.z);
     transform.LookAt(clampedTargetPos);
 }

I hope that gives you the clues you need.

Original Answer: I think you are very close! I've mocked this up and got it working (as I understand it) and I made a video which shows what I did, but I'll try to briefly explain here and if it doesn't make sense then go check out the video and ask more questions here.

It looks like you are using the approach that I first used for rotating around something (adjust the angle and then move over a bit then rinse and repeat) however I think you will enjoy knowing about the API called Transform.RotateAround() which I think is really what you are looking for.

Heres the code I used to mock this up in my video:

 public class AYRCubeOrbit : MonoBehaviour {
 
     public Transform t;
     public Transform ObjectToOrbit;
     public Vector3 RotationDirections;
     public float speed;
 
     void Update() {
         t.RotateAround (ObjectToOrbit.position, RotationDirections, speed * Time.deltaTime);
     }
 }


Using that code I was able to just put the following into the inspector:

  1. The orbiting objects transform and

  2. The transform of the object to orbit around and then

  3. Give it a Rotation Direction around the X Axis.

    Heres the video so you can see what this does. I hope it helps but remember just let me know if it doesn't and we will figure it right out! http://youtu.be/iW11IwzjR2o?hd=1

Comment
Add comment · Show 3 · 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 anarhist1994 · Nov 05, 2017 at 05:45 PM 0
Share

Thanks for your comment but it's not what I'm looking. If I remove the line "toPos = new Vector3(..." then when I rotate the main camera on any axis, the cube follows smoothly. BUT, What I want to achieve is that the cube won't move when I rotate the camera on X axis. Ins$$anonymous$$d, what happends is that when after some rotation on X axis of the camera, the object moves towards camera's position.(It moves on Z-axis)

avatar image anarhist1994 · Nov 07, 2017 at 12:59 PM 0
Share

I've updated my post. Can you take a look please?

avatar image FabDynamic anarhist1994 · Nov 15, 2017 at 03:00 AM 0
Share

Yeah I looked (sorry it took me so long). I plugged this code into my model and I agree that it doesn't work, but I'm not getting the exact same results so I bet I have something wrong.

Can you make a phone-video or screencast of what its doing for you and give us a tour of what your inspectors look like?

http://screencast-o-matic.com

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

72 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

Related Questions

Issues rotating around transform.up 2 Answers

Keep AI from flipping? 1 Answer

Rotation gameobject 0 Answers

How can I get Rigidbody.MoveRotation to rotate in world space on two axes? 1 Answer

How to rotate columns of a Rubik's cube? 2 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