Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ARTIFICIALINNOVATION · Nov 01, 2021 at 01:19 PM · tagscamera follow

Camera not following a GameObject with Tag after spawned in the scene?

I am making a racing game in which the player has to select the car and it loads the scene. The camera has FindWithTag("Player").transform;. But, when the prefab spawns as a prefab(clone) The camera, is not able to follow the cars.

 #pragma warning disable 649
 namespace UnityStandardAssets.Utility
 {
     public class SmoothFollow : MonoBehaviour
     {
         public CarUIClass CarUI;
         private int gearst = 0;
         private float thisAngle = -150;
 
         // The target we are following
         [SerializeField]
         private Transform target;
         // The distance in the x-z plane to the target
         [SerializeField]
         private float distance = 10.0f;
         // the height we want the camera to be above the target
         [SerializeField]
         private float height = 5.0f;
 
         [SerializeField]
         private float rotationDamping;
         [SerializeField]
         private float heightDamping;
         private VehicleControl carScript;
 
         // Use this for initialization
         void Start() 
         {
             target = GameObject.FindWithTag("Player").transform;
         }
 
         // Update is called once per frame
         void LateUpdate()
         {
             // Early out if we don't have a target
             if (!target)
                 return;
 
             // Calculate the current rotation angles
             var wantedRotationAngle = target.eulerAngles.y;
             var wantedHeight = target.position.y + height;
 
             var currentRotationAngle = transform.eulerAngles.y;
             var currentHeight = transform.position.y;
 
             // Damp the rotation around the y-axis
             currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
             // Damp the height
             currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
             // Convert the angle into a rotation
             var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
 
             // Set the position of the camera on the x-z plane to:
             // distance meters behind the target
             transform.position = target.position;
             transform.position -= currentRotation * Vector3.forward * distance;
 
             // Set the height of the camera
             transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
 
             // Always look at the target
             transform.LookAt(target);
             
         }
         public void CarAccelForward(float amount)
     {
         carScript.accelFwd = amount;
     }
 
     public void CarAccelBack(float amount)
     {
         carScript.accelBack = amount;
     }
 
     public void CarSteer(float amount)
     {
         carScript.steerAmount = amount;
     }
 
     public void CarHandBrake(bool HBrakeing)
     {
         carScript.brake = HBrakeing;
     }
     [System.Serializable]
     public class CarUIClass
     {
 
         public Image tachometerNeedle;
         public Image barShiftGUI;
 
         public Text speedText;
         public Text GearText;
 
     }
         public void ShowCarUI()
     {
         gearst = carScript.currentGear;
         CarUI.speedText.text = ((int)carScript.speed).ToString();
 
         if (carScript.carSetting.automaticGear)
         {
             if (gearst > 0 && carScript.speed > 1)
             {
                 CarUI.GearText.color = Color.white;
                 CarUI.GearText.text = gearst.ToString();
             }
             else if (carScript.speed > 1)
             {
                 CarUI.GearText.color = Color.white;
                 CarUI.GearText.text = "R";
             }
             else
             {
                 CarUI.GearText.color = Color.white;
                 CarUI.GearText.text = "N";
             }
 
         }
         else
         {
 
             if (carScript.NeutralGear)
             {
                 CarUI.GearText.color = Color.white;
                 CarUI.GearText.text = "N";
             }
             else
             {
                 if (carScript.currentGear != 0)
                 {
                     CarUI.GearText.color = Color.white;
                     CarUI.GearText.text = gearst.ToString();
                 }
                 else
                 {
 
                     CarUI.GearText.color = Color.white;
                     CarUI.GearText.text = "R";
                 }
             }
 
         }
 
 
 
 
 
         thisAngle = (carScript.motorRPM / 20) - 175;
         thisAngle = Mathf.Clamp(thisAngle, -180, 90);
 
         CarUI.tachometerNeedle.rectTransform.rotation = Quaternion.Euler(0, 0, -thisAngle);
         CarUI.barShiftGUI.rectTransform.localScale = new Vector3(carScript.powerShift / 100.0f, 1, 1);
 
     }
 
     
     }
 }
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

Answer by Betina_art · Nov 01, 2021 at 01:42 PM

Hi! Have you considered using Cinemachine? You can simply pass in the transform of the selected car to cinemachine's Follow and LookAt and it should follow the specified car automatically:


 using Cinemachine;
 
 public CinemachineVirtualCamera camera; //just attach the cinemachine camera here from the editor
     
     WhicheverMethod()
     {
         vcam.LookAt = this.transform;
         vcam.Follow = this.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

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

132 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 avatar image avatar image

Related Questions

How to get a list of gameobject tags in runtime 1 Answer

how to detect parent tag and not continually add numbers 1 Answer

Two Objects in the same position? 0 Answers

Score text is not updating on collision 1 Answer

How to make the Main Camera attached to a Car in the Car Tutorial 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