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 /
  • Help Room /
avatar image
0
Question by frpm1001 · Aug 01, 2019 at 05:01 PM · cameraplayerspawning problems

How to spawn a player and a camera together?

Hello there!

I've just recently started to try to develop games and stuff, and i have been wondering how can i spawn a camera that actually follows my also spawned player. I've managed to spawn a player, but i can't figure out how to attach a camera to it, because if i simply attach the player prefab together with the camera (or cm) prefab, they just don't work out.


My player is spawned together with my map, with both of them being generated by an image, where it's pixels are converted to blocks prefabs.

Is there any kind of coding (C#) or Unity tool that can do the trick?


I've also tried the following code, but it doesn't seems to be working properly and i'm honestly kinda lost on what to do with it, since i used this video as a base to build my code. Anyway, here's the code. Thanks in advance!

 using UnityEngine;
 
 public class TargetPlayer : MonoBehaviour
 {
     public Transform target;
        
     void Update()
     {
         FindPlayer();
         transform.LookAt(target);
     }
 
     void FindPlayer()
     {
         GameObject findPlayer;
         findPlayer = GameObject.FindGameObjectWithTag("Player");
         target = findPlayer.transform;    
     }


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

Answer by frpm1001 · Aug 02, 2019 at 06:19 AM

To everyone following this question or to whoever sees it, i managed to fix it. It still isn't as smooth as it would be with a CM, but it's working atleast.

If you know how to smooth the camera movement, please let me know! Thanks in advance!


I created an Empty Object, and then made the Camera and the Player it's children. Then, I added a "Player" tag do the Player and created the camera script as it is below. I'd also recommend you to set the offset to a negative value (of atleast -10) so that the camera isn't attached to your player. And then, i made the Empty Object my Prefab!

 using UnityEngine;
 
 public class CameraController : MonoBehaviour
 {
     public Transform target;
     public Vector3 offset;
   
     void Update()
     {
         FindPlayer();
         transform.LookAt(target);
         transform.position = target.position + offset;
         transform.rotation = Quaternion.Euler(Vector3.zero);
     }
 
     void FindPlayer()
     {
         GameObject findPlayer;
         findPlayer = GameObject.FindGameObjectWithTag("Player");     
         target = findPlayer.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
1

Answer by ahakra · Aug 01, 2019 at 10:53 PM

attach this script to the camera

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class followcomponent : MonoBehaviour {
 
  public   Transform player;
     public Vector3 offset;
     // Update is called once per frame
     void Update () {
        transform.position = player.transform.position + offset;
       
        
     }
 }

the offset to keep a distance off player if you needed it

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 frpm1001 · Aug 02, 2019 at 12:47 AM 0
Share

Hello there! Thanks for the quick reply!

Apparently it didn't work! I believe that it would work if you had your Player on the editor, but in my case, the player is spawned once the game has started!

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

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

I'm having issues with collision and Physics 1 Answer

Rotate player to camera direction when using cinemachine FreeLook Camera 0 Answers

Fixing camera for when player is oriented upside down or otherwise. 0 Answers

Trying to connect camera movement with player AND mouse 0 Answers

Camera flickers while chasing the player 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