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 /
avatar image
0
Question by centaurianmudpig · Sep 22, 2011 at 11:02 PM · rotationpositionpositioningrotatingtranslation

Offset position to a target

Hello,

I am fairly new to Unity and muddling my way through. I am having trouble trying to position the camera to an offset position of its target. The code below puts the camera just behind the target and works well enough when translating. The problem I have is when rotating the object. I want the camera to remain in the offset position, based on the objects rotation.

While the camera stays with the target, if you rotate the camera position does not rotate with it (it should change the position of the camera based on the position and rotation of the target).

To put this in another context, think of missiles attached to the wings of a fighter craft which are offset to the fighter's body. When the fighter rotates I would want the missiles to stay attached, in the exact position, on the wings.

I hope this makes sense.

 public class CameraView : MonoBehaviour {

 public Transform target;

 
 void Update() {
 }

 void LateUpdate () {
         float pitchAngle = 0 * Mathf.Deg2Rad;
         float yawAngle = 0 * Mathf.Deg2Rad;
         float radius = 0;
         float depth = -30;

         transform.position = target.position + new Vector3(Mathf.Cos(pitchAngle + target.rotation.x),
                                   Mathf.Sin(pitchAngle + target.rotation.y),
                                   0) * radius;

         transform.position = transform.position + new Vector3(0, 0,
                                   Mathf.Cos(yawAngle + target.rotation.z)) * depth;
 }

}

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

3 Replies

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

Answer by DaveA · Sep 22, 2011 at 11:09 PM

The easy thing would be to have the camera be a child of the object, then it will move/rotate with it. But short of that, if you want them separate, you could put an empty 'dummy' object there, and then in an Update function, read it's current world position/rotation and copy that to the camera's object's transform.

Comment
Add comment · Show 1 · 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 Skunk-Software · Jan 29, 2018 at 01:45 AM 0
Share

Parenting the camera to the player is the simplest way.

BUT if you don't want to:

Cam follows directly behind player (C#)

 Vector3 baseOffset = new Vector3(x, y, z); // The offset you start with (cam postion - player position)


             Vector3 staticOffset = Quaternion.AngleAxis(playerObj.transform.eulerAngles.y, Vector3.up) * baseOffset;
             offset = staticOffset;
 
             transform.position = playerObj.transform.position + offset;
 
             transform.LookAt(playerObj.transform.position);

avatar image
1

Answer by aldonaletto · Sep 22, 2011 at 11:33 PM

This will not work because you're using rotation components as if they were angles - and they aren't: rotation is a quaternion, and its (x y z w) components have nothing to do with the familiar angles we know. You should use localEulerAngles instead, but even this would not be the best way.
The easier way to do what you want is to child the camera to the target - it will follow the target position and rotation but always keeping its relative position and rotation. You can even rotate the camera if you want - child changes don't affect the parent object.
If you don't want to child the camera, however, use the target.forward vector as an initial reference, multiply by depth and apply a rotation based on your angles - that's your world displacement: add it to the target position and assign the result to the camera's position. You can just copy the target rotation to keep the camera aligned with the target direction.

public class CameraView : MonoBehaviour {

public Transform target;

void LateUpdate () { float pitchAngle = 0; float yawAngle = 0; float radius = 0; float depth = -30;

    Vector3 displacement = target.forward * depth; 
    displacement = Quaternion.Euler(-pitchAngle, 0, -yawAngle) * displacement;
    transform.position = target.position + displacement;
    transform.rotation = target.rotation;

}

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 centaurianmudpig · Sep 23, 2011 at 03:34 PM

Thanks to both for your answers. I tried parenting the camera and it works for what I need.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

blocks being placed in an odd way 0 Answers

rotating to the current facing position 2 Answers

Aligning a transform according to two different positions and directions 1 Answer

Floating-point errors while assigning transforms 1 Answer

Sphere "skids" on turns without rotating 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