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 /
This question was closed Aug 05, 2020 at 03:15 PM by stixy_boi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by stixy_boi · Mar 29, 2020 at 03:39 PM · camerascript.camera followcharacterscharacter customization

How to make the camera follow after the selected character?

i have a selection character script `using System.Collections; using System.Collections.Generic; using UnityEngine;

using UnityEngine.SceneManagement; public class skinselect : MonoBehaviour { private GameObject[] skinlist; public int index;

 // Start is called before the first frame update
 private void Start()
 {
     index = PlayerPrefs.GetInt("CharacterSelected");
     skinlist = new GameObject[transform.childCount];
     for(int i =0; i < transform.childCount; i++)
     
         skinlist[i] = transform.GetChild(i).gameObject;
     
     
     foreach(GameObject go in skinlist)
     {
         
         go.SetActive(false);
         if (skinlist[index])
             skinlist[index].SetActive(true);
     }
     
 }
 public void ToggleLeft()
 {
     skinlist[index].SetActive(false);
     index--;
     if (index < 0)
         index = skinlist.Length - 1;

     skinlist[index].SetActive(true);
 }
 public void Toggleright()
 {
     skinlist[index].SetActive(false);
     index++;
     if (index ==skinlist.Length)
         index = 0;

     skinlist[index].SetActive(true);
 }
 public void confirmbutton()
 {
     PlayerPrefs.SetInt("CharacterSelected", index);
 }

}

and camera follow script

 using UnityEngine;
 
 public class camerafollow : MonoBehaviour {
    
 
     
 
     public Transform player;
     public float smoothSpeed = 0.125f;
     public Vector3 offset;
     void LateUpdate()
     {
         transform.position = player .position + offset;
     }
 
 
 }
 

but i dont know how to make the camera follow after the selected character

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

  • Sort: 
avatar image
1

Answer by ahsen35813 · Mar 29, 2020 at 04:21 PM

I would recommend using this script. It works perfectly for me so it might be what you're looking for. Just make sure to add the intended player to be targeted into the public variable. This script will find the current location of the camera in relation to the targeted object and then maintain that location in relation to the target as the player moves around. If your player does not rotate, then you can just add the camera as a child of the player instead of needing this script.

 public class CameraController : MonoBehaviour
 {
     public GameObject Target;
 
     private Vector3 offset;
 
     // Start is called before the first frame update
     void Start()
     {
         offset = transform.position - Target.transform.position;
     }
 
     // Update is called once per frame
     void LateUpdate()
     {
         transform.position = Target.transform.position + offset;
     }
 }
 

If you want this script to automatically find the player, you can add this script into the Start() block.

 Target = gameObject.Find("PlayerName");

Replacing PlayerName with the name of the target for the camera and making the Target variable private.
I hope this helps!

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 stixy_boi · Mar 29, 2020 at 07:13 PM 0
Share

tnx a lot man, the first method is working, but it follows only after the character list, and in the list i have 3 skins of ball that is moving forward and the camera is staying with the list at the start.

and in the second method i can only put one skin name so thats not working

avatar image ahsen35813 stixy_boi · Mar 30, 2020 at 03:07 PM 0
Share

$$anonymous$$aybe an extra bit to the script that takes the variable of the skin selected to automatically set the target for the camera?

Something like this might work:

 private int skinSelection;
 
 public void SkinSelectionComplete()
 {
     skinSelection = gameObject.Find("BallList").GetComponent<SkinSelector>().skinSelected;
 if (skinSelection == 0)
     {
         Target = gameObject.Find("Red Ball");
     }
     else if 
     {
         Target = gameObject.Find("Green Ball");
     }
     else if 
     {
         Target = gameObject.Find("Blue Ball");
     }
 }
 
   

You will need a UI button that you click after selecting your player, and you can put SkinSelectionComplete() into the OnClick() area in the button. You will have to replace BallList with the name of the character list that you mentioned, replace SkinSelector with the name of the script on the ball list, and replace skinSelected with the public variable name containing the data about which skin was selected in the form of an int. Hope that helps!

Please note that I haven't tested this so you might have to do some debugging.

avatar image sacredgeometry · Mar 29, 2020 at 07:36 PM 0
Share

Why use that script when you can just use out of the box unity functionality? Its not doing anyting that parenting game objects doesnt offer with no code.

avatar image stixy_boi sacredgeometry · Mar 30, 2020 at 02:51 PM 0
Share

How to do it?

avatar image ahsen35813 sacredgeometry · Mar 30, 2020 at 02:57 PM 0
Share

Yes, but that only works if the player's orientation is not changing. For stixy_boi's situation, he has a rolling ball I think, so making the camera a child of the ball would make the camera roll around with the ball. You could give the ball zero friction to make that work, but that's not the right way to do it and this script gives more customizability as well.

Follow this Question

Answers Answers and Comments

210 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

Related Questions

change objects Axis to match cameras rotation 0 Answers

Display object panel in front of camera with correct rotation and position (no parent) 0 Answers

Check if position is on screen 0 Answers

URGENT Cinemachine virtual camera while falling 1 Answer

Camera follow player while it doesnt block animations 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