Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by lelis718 · Feb 04, 2017 at 07:42 AM · camerapositionorbit

Stuck with Camera Orbit Script

Hello I am stuck with this orbit camera script.

It is a third person camera that stays behind an AI character (no input controls) and follows it. In the future I intent to make it follow smoothly using some steering behaviours but first i need to get the correct position of camera (So no parent to AI object also).

The objective is control the camera with three variables.

CameraDistance - Controlling the distance from target

CameraAngleX - Controlling the Horizontal offset based on backward position of AI Object (if its 0 stays in the center, when increases or decreases, it moves left or right maintain the same CameraDistance for the AI)

CameraAngleY - Controlling the Vertical offset based on backward position of AI Object (if its 0 stays in the middle, it moves top or bottom maintaining the same CameraDistance for the AI)

Here is what i came up so far

     public float CameraDistance = 5f;
     public float CameraAngleX = 10f;
     public float CameraAngleY = 30f;
 
     // Update is called once per frame
     void Update()
     {
         var offset = new Vector3(0, 0, -CameraDistance);
 
         var cameraOrbitalRotation = Quaternion.Euler(CameraAngleY, CameraAngleX, 0);
 
         // Achamos a posição do objeto com base na multiplicação do Quaternion pelo Vetor e adicionamos ao nosso alvo
         var cameraDesiredPosition = Target.transform.position + (cameraOrbitalRotation*offset);
 
 
         transform.position = cameraDesiredPosition;
         LookForward();
 }

The Axis code actually works kind of expected but the camera is tracking only the object's position and when AI Object rotates it doesn't stay in back of it.

Anyone have suggestions?

EDIT 1 -

Ok, I think I've managed a "Workaround", it wont work as i expected but will fit in my problem.

The code that I've managed was a little what @Skyway-Interactive told. but instead of using target.transform.forward in position, i used it in Offset.

Here is the actual code

void Update() { // Use target's forward as offset to get the correct camera distance var offset = -Target.transform.forward * CameraDistance;

   //Find the horizontal rotation angle
   var cameraOrbitalRotation = Quaternion.Euler(0,CameraAngleX,0)*offset;
 
   // Apply to position and add the Vertical CameraAngleY
   var cameraDesiredPosition = Target.transform.position + (cameraOrbitalRotation) + (Target.transform.up*CameraAngleY);
   transform.position = cameraDesiredPosition;
 
   LookToTheTarget();
 }

The only problem that I've saw here is the CameraAngleY won´t obey the CameraDistance, That happens because I am adding it's position directly to the camera's transform. What I was intended to do is roll the Vertical Axis just like i did with the Horizontal one (CameraAngleX).

Any Suggestions?

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 Skyway-Interactive · Feb 04, 2017 at 08:24 AM

So it looks like you need to find the "spot behind the character" not just the characters position. I would think something like: target.transform.position + (target.transform.forward *-distance) I think your code is almost there.

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

117 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

Related Questions

UI Elements repositioning and resizing when playing game 1 Answer

How to make an orbiting camera around the TANKS! tutorial map? 0 Answers

Camera registering it is at correct position when it isn't 0 Answers

Camera out of position on building/cloning to other machine,Camera out of place on another machine 0 Answers

How get position from a game object? 5 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