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 sebeisdrache · Jan 20, 2015 at 11:39 AM · cameraparentfollow playersmoothfollow

Camera smoothing of a Child Camera

Hello,

I'm tyring to make a simple multiplayer spaceshooter. For the Spaceships I want a 3rd person camera just binhind and a little above the player ships. The Camera is atached to the Player Ship Prefab as an Child object.

Now I want a bit of smoothness in the camera's Motion Relative to its Parent object.

I only found some Scripts with Targets, but I have no Target, just looking forward.

Somebody knows a Solution for this? Smoothing the Cameras Position without a Target (only its Parent) to follow?

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 Wiki

Answer by DaDonik · Jan 20, 2015 at 11:44 AM

I would not parent the camera to the spaceship. The way i normally do this kind of stuff is as follows:

1) Use the forward vector of the spaceship, multiply it by minus the distance you want the camera to stay behind the ship.

2) Do the same with the up vector of the ship.

Now you have a position where you want the camera to move to. All thats left to do is to lerp the camera towards this position.

This will do the job, in fact id does even more than requested :D

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour 
 {
     public Camera TheCamera;
     public Transform TargetToFollow;
     public Vector3 DesiredOffset;
     public float Speed;
 
 
     void Update () 
     {
         // Calculate offset vector.
         Vector3 v3TargetOffset = TargetToFollow.position;
         v3TargetOffset += (DesiredOffset.z * TargetToFollow.transform.forward);
         v3TargetOffset += (DesiredOffset.y * TargetToFollow.transform.up);
         v3TargetOffset += (DesiredOffset.x * TargetToFollow.transform.right);

         // Lerp the camera.
         Vector3 v3CurrentCameraPos = TheCamera.transform.position;
         v3CurrentCameraPos.x = Mathf.Lerp(v3CurrentCameraPos.x, v3TargetOffset.x, Speed * Time.deltaTime);
         v3CurrentCameraPos.y = Mathf.Lerp(v3CurrentCameraPos.y, v3TargetOffset.y, Speed * Time.deltaTime);
         v3CurrentCameraPos.z = Mathf.Lerp(v3CurrentCameraPos.z, v3TargetOffset.z, Speed * Time.deltaTime);
         TheCamera.transform.position = v3CurrentCameraPos;
         TheCamera.transform.LookAt(TargetToFollow.transform);   
     }
 }

Comment
Add comment · Show 5 · 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 sebeisdrache · Jan 20, 2015 at 04:41 PM 0
Share

Thank you, it works well for me, i just make some changes.

I had problems with some stutter, it cam be solved by putting the code into the "FixedUpdate" function.

 using UnityEngine;
 using System.Collections;
 public class CameraFollow : $$anonymous$$onoBehaviour
 {
     public Camera camera;
     public Transform target;
     public Vector3 offset;
     public Vector3 viewPointOffset;
     public float speed;
 
     public bool ActivateSmoothRotation;
 
     void FixedUpdate ()
     {
         if(camera && target)
         {
             Vector3 v3TargetOffset = target.position;
             v3TargetOffset += (offset.z * target.transform.forward);
             v3TargetOffset += (offset.y * target.transform.up);
             v3TargetOffset += (offset.x * target.transform.right);
 
             Vector3 v3viewPointOffset = target.position;
             v3viewPointOffset += (viewPointOffset.z * target.transform.forward);
             v3viewPointOffset += (viewPointOffset.y * target.transform.up);
             v3viewPointOffset += (viewPointOffset.x * target.transform.right);
 
             if(ActivateSmoothRotation)
             {
                 Quaternion rotation = Quaternion.LookRotation(v3viewPointOffset - camera.transform.position);
                 Quaternion smoothRotation = Quaternion.Slerp(camera.transform.rotation, rotation, Time.deltaTime * speed);
                 camera.transform.rotation = smoothRotation;
             }
 
             camera.transform.position = Vector3.Lerp(camera.transform.position, v3TargetOffset, Time.deltaTime * speed);
 
             if(!ActivateSmoothRotation)
                 camera.transform.LookAt(v3viewPointOffset);
         }
     }
 }
avatar image sebeisdrache · Jan 20, 2015 at 10:25 PM 0
Share

Hello again, is there a way to apply the Z rotation to the Camera? If I make

 smoothRotation.z = target.rotation.z;

I got a very Strange behavior If i Rotate the ship around this Axis.

avatar image DaDonik · Jan 20, 2015 at 10:57 PM 0
Share

Let me guess, thats only the case if you are using the LookAt function?

If so, the problem can be solved by using the second parameter of the LookAt function. Check the documentation here

So when using LookAt, you should not try to rotate the camera around the z-axis, ins$$anonymous$$d you should specify the up vector in the LookAt function.

avatar image sebeisdrache · Jan 20, 2015 at 11:30 PM 0
Share

ok, thats it Thank you again.

The second parameter works also with the Quaternion.LookRotation function.

avatar image DaDonik · Jan 20, 2015 at 11:40 PM 0
Share

I'm so proud of you, so new to Unity, yet such a quick learner :D

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

Why is my Camera rotating? 2 Answers

Smooth camera script looking at left hand side of character. 0 Answers

Child affecting parent rigidbody? 1 Answer

How to smoothen movement of a child object? 1 Answer

Target Box not bounding targets 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