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 lullaharsh · Dec 27, 2016 at 12:44 PM · cameratargetthird-person

Changing Free Look Camera Target At Runtime

Question says it all! Actually I spawn a character with UNET and I need to set him as a target on the camera after I spawn him. Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dustypantz · May 02, 2017 at 08:56 AM

public Transform Target { get { return m_Target; }}

No setter for Target property. Use "SetTarget(Transform newTransform)"

example: LocalCamera.GetComponent().SetTarget(transform);

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 tashfiq103 · Dec 18, 2019 at 09:17 AM

I hope the following code will solve the problem :)

 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class CameraLook : MonoBehaviour
 {
 
     #region Public Variable
 
     //Setting : A default camera target, so it can look at something when the game is start
     public Transform defaultCameraTarget;
 
     [Space(5.0f)]
     //Value : How fast your camera will follow the target
     [Range(0f,1f)]
     public float cameraAngulerVelocity = 0.05f;
 
     #endregion
 
     #region Private Variables
 
     private Transform m_TransformReference;
     //Reference : We are actually using another transform reference so that we can change the camera target in runtime.
     private Transform m_CurrentCameraTarget;
     //Note : What should be the rotational value to look at to the following target.
     private Quaternion m_LookDirection;
     //Note : Modified Rotational value based on your angulerVelocity
     private Quaternion m_ModifiedRotation;
 
     #endregion
 
 
     private void Start()
     {
         m_TransformReference    = transform;
         m_CurrentCameraTarget   = defaultCameraTarget;
     }
 
     private void LateUpdate()
     {
         //If : The current camera target is destroyed, it will switch back to the default one.
         if (m_CurrentCameraTarget == null)
             m_CurrentCameraTarget = defaultCameraTarget;
 
         m_LookDirection = Quaternion.LookRotation(m_CurrentCameraTarget.position - m_TransformReference.position);
         m_ModifiedRotation = Quaternion.Slerp(
                 m_TransformReference.rotation,
                 m_LookDirection,
                 cameraAngulerVelocity
             );
 
         m_TransformReference.rotation = m_ModifiedRotation;
     }
 
     /// <summary>
     /// Call the function by passing the transform reference if you want to change the camera angle through runtime
     /// </summary>
     /// <param name="t_CameraTarget"></param>
     public void ChangeCameraTarget(Transform t_CameraTarget) {
 
         m_CurrentCameraTarget = t_CameraTarget;
     }
 
 }

@lullaharsh

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Third Person Flight Camera HELP!! 1 Answer

Third Person Camera Help? 0 Answers

Camera and Target JS to CS converting 2 Answers

How can I mimic this game's camera in Unity? 1 Answer

Set Camera Target Error .. 0 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