Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pp19weapon · Oct 12, 2014 at 02:58 PM · transformpositioncamera-movementmoveangle

How to move camera on an angle

I'm trying to rotate the camera around a box, like when the box moves on the X angle, the camera will have a position on the X angle, following the box, but If the player press "D" the box starts to move on the Z angle and the camera needs to rotate around that box to have a position at the back of the box.

My script(as you can see,I tried to rotate the camera manually, but I failed):

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public GameObject cube;
     public float rotateSpeed = 5;
     Vector3 offset;
 
     // Use this for initialization
     void Start () {
         offset = cube.transform.position - transform.position;
     }
     
     // Update is called once per frame
     void Update () {
         
         //if (CharachterControll.movingDirect == 1) {
             //transform.position = new Vector3(cube.transform.position.x - 12, cube.transform.position.y + 8, transform.position.z);
         //}
         //else if (CharachterControll.movingDirect == 2)
        // {
             //transform.position = new Vector3(cube.transform.position.x, cube.transform.position.y + 8, transform.position.z - 12);
         //}
     }
 
     void LateUpdate()
     {
         if (CharachterControll.movingDirect == 1)
         {
             //float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
             //cube.transform.Rotate(0, horizontal, 0);
 
             float desiredAngle = cube.transform.eulerAngles.y;
             Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
             transform.position = cube.transform.position - (rotation * offset);
         }
         else if (CharachterControll.movingDirect == 2)
         {
             float desiredAngle = cube.transform.eulerAngles.y;
             Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
             transform.position = cube.transform.position - (rotation * offset);
         }
         transform.LookAt(cube.transform);
     }
 }
 

(Sorry for my english)

Comment
Add comment · Show 2
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 robertbu · Oct 12, 2014 at 03:43 PM 0
Share

I don't understand what you are asking. Often language gets in the way of understanding rotation problems. A drawing might help.

avatar image pp19weapon · Oct 12, 2014 at 04:02 PM 0
Share

Well I tried... Well, I tried...

1 Reply

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

Answer by robertbu · Oct 12, 2014 at 05:30 PM

I'm out of time for a detailed answer. It can be done with a single game object, but a simple solution can be done with two objects:

  • Create an empty game object and place it at the center of the box. The empty game object's initial rotation must match the box.

  • Make the camera a child of the empty game object at the right distance and looking at the box.

  • Add a line of code in LateUpdate() that has the empty game object tracking the box (it is not a child, simply following the position of the box).

  • Have the empty game object rotate to look at the direction of movement.

  • Slerp or Lerp the rotation as necessary.

Comment
Add comment · Show 2 · 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 pp19weapon · Oct 12, 2014 at 07:14 PM 0
Share

Thank you very much, it worked!

avatar image purecrafttube · Jul 05, 2016 at 08:14 PM 0
Share

please can someone explain the last 3 lines of instructions for a noob please

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Vector 3 - move on only one axis 0 Answers

How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 1 Answer

move a gameobject to left/right while going up 1 Answer

Vector3.Slerp not working 2 Answers

C# move y position of object not working 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