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 MoonlitDay · Jul 02, 2015 at 05:01 AM · c#transformpositionvector3

Script to make Camera follow the player C#

Hello, why does my script for the camera not work? I want it to follow the player, and I thought, theoretically it should. Here's my code for the Main Cam.

 using UnityEngine;
 using System.Collections;
 
 public class CamControls : PlayerControl {

     void Update () {
         transform.position = new Vector3 (xPos, yPos, zPos);
     }
 }

It inherits from my PlayerControl script that determins xPos, yPos and zPos:

         xPos = transform.position.x;
         yPos = transform.position.y;
         zPos = transform.position.z;

From my understanding of my camera script, it should move where the player currently is. Sorry if this is a silly question, but having an answer to this would help me learn c# more. Thanks!

Comment
Add comment · Show 1
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 Hrungdak · Jul 02, 2015 at 05:38 AM 0
Share

The playerControl-Class may have the variables, but where are the variables set to any value? Show the playerControl-Class.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dlonyar · Jul 02, 2015 at 11:30 AM

You could just make the MainCamera object a child of the player object.

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 $$anonymous$$ · Oct 24, 2015 at 01:31 PM

There are two possible problems making a camera the child of the player object. One, if your player is destroyed, the camera is destroyed. Second, if this occurs it will lock up the game and if you have such as a GameController where it has a gameOver function, if the character/player is destroyed, with the camera, it will lock down the game and potentially crash it.

One example of a possible script might be present in the old Project Stealth tutorial.

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 TrickyHandz · Oct 24, 2015 at 04:29 AM

Inheriting from the PlayerControl class will not link an instance of the CameraControl class to an instance of the base class. Inheritance simply means that the CamControls class has all the properties and methods of the PlayerControl class. You would still need to assign references to the player object in your game for the camera to execute follow behaviors.

In reference to inheritance in C#, I would recommend reading over the MSDN Article on the subject: Inheritance (C# Programming)

Regarding camera follow behaviors, I recommend this tutorial to get started with 3rd Person Cameras (Note it is from 2012, but is still very informative)

TutsPlus: Unity Third Person Camera

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to rotate an object to face the direction it's going? 1 Answer

Trouble setting up a vector 3. 1 Answer

Guided missile help c# 1 Answer

Issue's With Positioning 0 Answers

How to store position in a variable and than edit only specific part of it(such as 'x') 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