Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 actionforgames · Jul 30, 2015 at 06:26 PM · booleanmovingoverlaprotating

Rotate camera around moving object

Hi so I started today with Unity and I have some big progress. however I have struggle to make rotation about moving object and follow it at the same time. My code so far:

 function Start () {
 
 }
 var gObj = GameObject.Find("ball");
 var target : Transform;
 var rotationspeed = 40;
 
 function Update () {
 transform.LookAt(target);
 if (Input.GetKey(KeyCode.A)){
 transform.RotateAround (gObj.transform.position,Vector2.up,rotationspeed * Time.deltaTime);
 }
 if (Input.GetKey(KeyCode.D)){
 transform.RotateAround (gObj.transform.position,Vector2.up,-rotationspeed * Time.deltaTime);
 }
 }

So with that code I make using A and D to rotate around ball and Looking At it at same time. Now in my case ball will move around and Camera suppost to follow it and keep same distance from ball all the time.

Comment
Add comment · Show 1
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 Positive7 · Jul 30, 2015 at 07:00 PM 0
Share

How about make the camera child of the ball?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by OrbitGames · Jul 30, 2015 at 07:18 PM

if you want to do it using a script, first you need to rotate the Camera around the y axis based on key Input, then set its position to the gameObjects position - transform.forward * the distance between the GOs In code it should look something like this(sorry for c#, untested)

 if (Input.GetKey(KeyCode.A)){
 tranform.Rotate(0,rotationSpeed*Time.deltaTime,0);
  }
  if (Input.GetKey(KeyCode.D)){
 tranform.Rotate(0,-rotationSpeed*Time.deltaTime,0);
  }
 transform.position = target.position - transform.forward * 15;

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 _Gkxd · Jul 30, 2015 at 07:07 PM

While it's possible to do this using scripts, it's probably easier to parent transforms to each other.

The structure of the GameObjects would look something like this:

 Ball (Your GameObject)
 |-> Pivot Point (Empty GameObject, child of "Ball")
     |-> Camera (Your Camera, set position to <0, 0, -1>, rotation to <0, 0, 0>, scale to <1, 1, 1>)

Now, when you want to rotate your camera, rotate the "Pivot Point" GameObject instead. (Try doing this manually in the scene view first.)

The camera will automatically move around the ball. If you want to move your camera away/closer to the center, you can scale the "Pivot Point" GameObject.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keyboard rotating - left is stopping object 1 Answer

Cube Rotating & Moving 1 Answer

rotating/moving colliders with or without rigidbodies? 2 Answers

Ball moving only diagonal 0 Answers

How to handle different bools in 1 statement 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