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 RyanZimmerman87 · Feb 16, 2014 at 08:36 AM · camerarotationpositionchild objectparent transform

How would you set a camera lerp position with code not a child object?

I've run into this problem multiple times, being unable to set positions and rotations of a "child" object relative to moving and rotating "parents" with simply code instead of having to use a child object. I want to know how to do this without using child objects to artificiality solve this for me.

So lets say I'm trying to create a new promo video clip which has a camera following a pet while it kills enemies kind of a "Kill Cam".

I just tried to set this up and I'm always trying to improve my programming so naturally I start by trying to set the camera's rest position with code. But it seems every time I'm forced to just set up a child object attached to the parent to get the proper positions and rotations to work for me.

Can someone do a code example of how you could have similar results to always have the behaviors of a child object without it actually being a "child" object? I want the correct position and rotation relative to the parent for every frame without needing to use a child object.

Every single time I've tried this I end up having to use a stupid child object and it's really pissing me off I can't do it with just math or what seems like incredibly simple programming.

I feel like I'm missing a very simple and essential aspect of programming if I can't do this. I feel like using a child object through Unity is like a cheap short cut to true understanding of what I'm doing, I'd prefer to know how to do this without relying on a child object which is automatic.

Code Example:

 //this script is on the camera
 void LateUpdate() 
 {
 
 //temporary messy solution for promo video
 if (petObject == null)
 {
 petObject = GameObject.Find("Pet Baby Blue Beast");
 
 petCameraTransform = GameObject.Find("Pet Camera Object").transform;
 
 petCameraVector = petCameraTransform.position + new Vector3 (0, 2, 0);
 
 }


 //startTransform is just the camera's Transform
 endDirectionVector = (petObject.transform.position + new Vector3 (0, 2, 0) - startTransform.position).normalized;
 
 endRotationQuaternion = Quaternion.LookRotation(endDirectionVector);
 
 startTransform.rotation = Quaternion.Slerp (startTransform.rotation, endRotationQuaternion, .5f);
 
 startTransform.position = Vector3.Lerp (startTransform.position, petCameraTransform.position, .01f);
 }


So the problem here is that I'm relying on the "Pet Camera Object" which gives me my petCameraTransform and resulting petCameraVector variables.

How can I do this without using a child object for the "Pet Camera Object"?

I feel like I've just somehow tried every single wrong possible combination of code to solve this without a child object. It seems the vector positions always get screwed up relative to the parent object whenever I try to simulate the child.

Maybe I'm over-thinking things and should just use the child object, but it really bothers me that I HAVE to use the child object.

I've had similar problems with other applications like visual blood effects where I feel I shouldn't need a child object to do it.

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
1
Best Answer

Answer by Scribe · Feb 16, 2014 at 04:25 PM

This doesn't include any smoothing but it does find the position the camera should be at and rotates the camera to face your object + some offset.

 var target : Transform;
 var followOffset : Vector3 = Vector3(0, 2, -5);
 var lookOffset : Vector3 = Vector3(0, 1, 0);
 
 private var localFollow : Vector3;
 private var localLook : Vector3;
 
 function Update () {
     localFollow = target.position + target.TransformDirection(followOffset);
     localLook = target.position+target.TransformDirection(lookOffset);
     transform.position = localFollow;
     transform.LookAt(localLook);
 }

I haven't taken a look in quite a while but I seem to remember that the standard asset 3rd person camera controller does something similar to this (though a lot more advanced) but that might be a good place to look if this doesn't help.

Scribe

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 RyanZimmerman87 · Feb 18, 2014 at 06:06 AM 0
Share

That looks promising!

Wow looks like there is a specific Unity Function TransformDirection built right in to do exactly what I was trying to do with all my failures ^^

Thanks for answer, I can't believe I never knew about that, can't even remember seeing it in any of the 100's of posts I've read here.

I'll have to give it a try next time I set something like this up, Gonna accept your answer even though I didn't test it yet.

I'd still be curious how to do it with just the raw math involved ins$$anonymous$$d of a Unity Function but I guess that's just not worth it even these days.

avatar image Scribe · Feb 18, 2014 at 12:56 PM 0
Share

The raw maths of the position of the object is quite easy, the following code should achieve the effect:

 var target : Transform;
 var followOffset : Vector3 = Vector3(0, 2, -5);
 private var localFollow : Vector3;
 
 function FollowPosition(obj : Transform, offset : Vector3) : Vector3{
     var returnVector : Vector3 = obj.position+(obj.right*offset.x)+(obj.up*offset.y)+(obj.forward*offset.z);
     return returnVector;
 }
 
 function Update () {
     localFollow = FollowPosition(target, followOffset);
     transform.position = localFollow;
 }

The rotation part is quite a bit harder if you want to do it completely from scratch but still do-able and can be done with some classical mechanics. If you don't $$anonymous$$d using some other built-in stuff you can do it by using the same FollowPosition method from above:

 function Update(){
     transform.forward = FollowPosition(target, lookOffset)-transform.position;
 }

If you aren't happy with setting the forward of the transform (as it is basically the same as using LookAt) you will have to use some mechanics as I mentioned above. I have not checked this so I may be wrong but I think this is what you would need to do:

  1. First find the direction from your following object to the point it should look at (the same as I did just above)

  2. Then get the axis that is orthoganal (90degrees to) the direction you just found and the object that you are followings forward direction. To do this you can use the cross product of 2 vectors, Unity has this built in (see Vector3.Cross)

  3. Find the angle that you need to rotate around this axis by finding the dot product, dividing by the magnitude of the vectors mutiplied and taking the arccosine. Something along the lines of $$anonymous$$athf.Acos(Vector3.Dot(direction.normalized, obj.forward))*$$anonymous$$athf.Rad2Deg (See Vector3.Dot)

  4. Then you can get a rotation from this angle axis notation using Quaternion.AngleAxis

  5. If your still not satisfied using the builting ANgleAxis operation then check out this link, there is also some code slightly further down the page which you can convert to Unityscript/C#

If you have trouble following any of that let me know, I may try to do this myself from raw maths as it seems like a good exercise for my mechanics!

Have fun,

Scribe

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

19 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

Related Questions

How to make Camera position Independent of its Rotation? 1 Answer

How to apply a force forward depending of the camera 2 Answers

How to always face player relative to player's rotation and how do I use TransformDirection to move a player regardless of Y-rotation? 0 Answers

How to make camera move backwards in relation to player (so behind player), regardless of y rotation of player and camera 1 Answer

Having a 3D text appear in the middle of the screen without using GUI (C#) 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