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 /
This question was closed Aug 11, 2014 at 07:49 AM by RedDevil for the following reason:

I ended up just increasing the 2D Sky Texture as it was too much trouble for just a little thing.

avatar image
0
Question by RedDevil · Aug 11, 2014 at 07:08 AM · cameratransformposition

How to make Main Camera to not follow object on Y

Hi.So i have this 2D game where my character can move and jump and I want my camera to follow him on the X position but it also follows him on the Y.I have stoped this by changing the camera Y position every frame.This does not really help because when the character jumps the camera strarts to vibrate heavly. The script is from the sample assets:

 using UnityEngine;
 using System.Collections;
 
 public class Camera2DFollow : MonoBehaviour {
     
     public Transform target;
     public float damping = 1;
     public float lookAheadFactor = 3;
     public float lookAheadReturnSpeed = 0.5f;
     public float lookAheadMoveThreshold = 0.1f;
     float offsetZ;
     Vector3 lastTargetPosition;
     Vector3 currentVelocity;
     Vector3 lookAheadPos;
     
     // Use this for initialization
     void Start () 
     {
         lastTargetPosition = target.position;
         offsetZ = (transform.position - target.position).z;
         transform.parent = null;
     }
     
     // Update is called once per frame
     void Update () 
     {
         this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x,3.898071f,this.gameObject.transform.position.z);
         // only update lookahead pos if accelerating or changed direction
         float xMoveDelta = (target.position - lastTargetPosition).x;
 
         bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
 
         if (updateLookAheadTarget) {
             lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
         } else {
             lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);    
         }
         
         Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
         Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
         
         transform.position = newPos;
         
         lastTargetPosition = target.position;        
     }
 }

Making the Y position change in the Fixed Update causes even more vibrations;

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 lord_benry · Nov 14, 2018 at 04:22 PM 0
Share

$$anonymous$$y code for only moving on x is :

using UnityEngine;

public class FollowCam : $$anonymous$$onoBehaviour {

 public GameObject Player;
 public GameObject Camera;

 private float playerPosX;

 void Update()
 {
     playerPosX = Player.transform.position.x;
     Camera.transform.position = new Vector3(playerPosX, Camera.transform.position.y, Camera.transform.position.z);
 }

}

3 Replies

  • Sort: 
avatar image
1

Answer by VIPINSIRWANI · Aug 11, 2014 at 07:29 AM

You Can try Like that it will work for you in my case its following boy for only Z Direction.

     public GameObject boy;
     public Transform Cam;
 
     void Update () {
         boy.transform.Translate(0,0,0.4f);
         Cam.transform.position = new Vector3(Cam.transform.position.x,Cam.transform.position.y,boy.transform.position.z - 5);    
     }

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 RedDevil · Aug 11, 2014 at 07:48 AM 0
Share

this make the boy go at high speed in one direction and falling through the map

avatar image
1

Answer by khaled235711 · Aug 11, 2014 at 09:32 AM

If you Just Need a X-Follow Script here is one put this script to the camera

 using UnityEngine;
 using System.Collections;
  
 public class Camera2DFollow : MonoBehaviour
 {
      public Transform Player;
      
      void Update()
      {
       transform.position = new Vector3(Player.position.x, transform.position.y, transform.position.z);
      }
 }
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 Subhajit-Nath · Aug 11, 2014 at 07:26 AM

Just store the initial Y position of the camera in the Start () method and use it in the follow vecotr's Y position.

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 RedDevil · Aug 11, 2014 at 07:28 AM 0
Share

i am not sure how to do that

avatar image Subhajit-Nath · Aug 11, 2014 at 07:38 AM 0
Share

Try following VIPINSIRWANI's example.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

Place a object in front the camera in the coordinate 0 of Y axis 2 Answers

Following a gameObject, unexpected problems? 1 Answer

Camera movement 1 Answer

Place object in front of me with given Translation Vector 0 Answers

Trouble converting transform.position to C# 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