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 Sam Mbois · Apr 25, 2014 at 12:17 PM · camerafollow

Follow Camera 3D Player

I have camera follow script. But when I attached this script to player, camera doesnt follow the camera. Help me please :) thank you

Regards, Tio.


 public Camera mainCamera;
 
 void Start(){
     mainCamera = GameObject.FindWithTag ("MainCamera").camera;
 }
 
 void Update () {
     
     mainCamera.transform.position = new Vector3 
         (transform.position.x, 1, -1);
 }
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 Sam Mbois · Apr 26, 2014 at 05:11 AM 0
Share

I've been try it, but it still not followed the player.

This is my player script. When I press UpArrow I want player move, play animation and camera will follow the player. But its still not working.

public float $$anonymous$$oveSpeed = 5.0f;

 void Update () {
     if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
         animation.Play ("run");
         transform.position += transform.forward * $$anonymous$$oveSpeed * Time.deltaTime;
     } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) {
         animation.Play ("jump");
         transform.position += transform.forward * $$anonymous$$oveSpeed * Time.deltaTime;
     } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
         animation.Play ("right");
         transform.position += transform.right * $$anonymous$$oveSpeed * Time.deltaTime;
     } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
         animation.Play ("left");
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by liortal · Apr 25, 2014 at 02:35 PM

I am not sure what you meant to do. In many cases, it is required that the camera will follow the player.

Here's a sample script (actually taken from Unity's 2D project sample):

 using UnityEngine;
 using System.Collections;
 
     public class FollowPlayer : MonoBehaviour
     {       
         private Transform player;        // Reference to the player.
     
         void Awake ()
         {
             // Setting up the reference.
             player = GameObject.FindGameObjectWithTag("Player").transform;
         }
     
         void Update ()
         {
             // Set the position to the player's position with the offset.
             transform.position = player.position;
         }
     }

This, when attached to the camera, will follow the player's movement.

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 Sam Mbois · May 21, 2014 at 06:18 AM 0
Share

Its work. but how can I setting the distance?

avatar image
1

Answer by socialspiel · Apr 25, 2014 at 01:41 PM

Use LateUpdate for this. LateUpdate is called after all Update functions on alle GameObjects have been called. So you make sure, that the camera is updated after all other movement has occured.

Furthermore check if you got the reference for the camera right. You could for easier access just use Camera.main for the first active camera.

 void LateUpdate () {
     Camera.main.transform.position = new Vector3 
        (transform.position.x, 1, -1);
 }
Comment
Add comment · Show 2 · 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 liortal · Apr 25, 2014 at 10:39 PM 0
Share

It may be a coding style preference, but i tend to like more code that modifies the camera's properties (transform) on the camera object, than having a script that modifies the camera's properties from another game object (the player).

avatar image socialspiel · May 02, 2014 at 09:51 AM 0
Share

in my code the script that controls the camera is attached to the camera. the op attaches the camera control script to the player and asked what might be wrong with his code. hence my answer.

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

21 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

Related Questions

Camera follow smoothness problem 1 Answer

camera follow an game object 0 Answers

2d camera follow player by looking ahead. 1 Answer

Camera Border Follow 1 Answer

Camera 2d following player with delay jumps 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