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 TwitchyOrphan · Nov 10, 2021 at 09:37 PM · follow player

SmoothFollow not following Instantiated object

Hello, I've tried to find a solution to this on here, but nothing fits to make my scene work as intended. SmoothFollow follows my GameObject as long as it's created before the game is ran, but when I try to attach the prefab, it won't follow anything. From what I've read, it's because the prefab will be a "clone" so it's named differently.

Long story short, how do I get the camera to follow my prefab player after it is Instantiated into the scene?

 public Transform target;
 public float camDist = 10.0f;
 public float height = 5.0f;    
 public float heightDamping = 2.0f;
 public float rotationDamping = 3.0f;

 void LateUpdate()
 {

     if (!target)
     {
         return;
     }

     float wantedRotationAngle = target.localEulerAngles.y;
     float wantedHeight = target.position.y + height;
     float currentRotationAngle = transform.localEulerAngles.y;
     float currentHeight = transform.position.y;

     currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

     currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

     var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

     transform.position = target.position;
     transform.position -= currentRotation * Vector3.forward * camDist;

     transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);

     transform.LookAt(target);

     CompensateForWalls();
 }

Thank you in advance!

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

Answer by swanne · Nov 10, 2021 at 10:00 PM

Hey @TwitchyOrphan
So, the camera needs to identify the target at runtime so we need a way to find it.
This is the way that I have done it and it worked ok for me. Tag the prefab with "Player" which carries through when the object is instantiated. Have the camera script search for an object with that tag and assign it to the target variable.
I used a TimeToSearch variable which allowed me build in a lag for when then player gets destroyed and has a 3 second lead time before being spawned again.

Give this a try:

 private float nextTimeToSeach = 0;
 
 private void Update()
 {
     if (target == null) 
     {
         if (nextTimeToSeach >= Time.time) 
         {
             GameObject player = GameObject.FindGameObjectWithTag("Player");
             if (player == null) return;
             target = player.GetComponent<Transform>();
         } 
         else 
         {
             nextTimeToSeach++;
         }
 
     }
     
     if (target == null) return;
 }

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 TwitchyOrphan · Nov 10, 2021 at 11:44 PM 0
Share

Thank you @Swanne! I'd be lying if I said I understood why this worked, but it did. I also see the value in this as I haven't created a "respawn" yet, but I feel like this will be very handy once I add the option to have the player respawn once out of health.

Thank you again!

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

130 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 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

Make object follow player on trigger 0 Answers

Following a gameObject, unexpected problems? 1 Answer

Camera keeps resenting after I'm no longer Locking ON target, and I don't know how to fix it. 0 Answers

Problem using DontDestroyOnLoad carrying over a player, but not a camera 2 Answers

How do I make my enemyAI rotate only left and right, not up and down 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