• 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 WalidMohamed · Aug 13, 2016 at 05:42 PM · unity 5cameratransformpositionoffset

Help! I want to make a following camera but in with Y position is constant ?

i wanna make the camera follow my object with fixed distance between them ,but without changing the y position (Distance of y axis between object and camera ) . Knowing that the object moves right and left and jumps thanks a lot.

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 EDevJogos · Aug 13, 2016 at 06:43 PM 0
Share

Can't you set the camera as a child of your object ?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Aug 13, 2016 at 07:09 PM

The most simple way I can think off is something like this...

 public class Follow extends MonoBehaviour {
     [SerializeField]
     Transform target;
     [SerializeField]
     Vector3 offset;
 
     void Update() {
         Vector3 newPosition = new Vector3(target.positions.x + offset.x, offset.y, target.position.z + offset.z);
         transform.position = newPosition;
     }
 }

Attach that script to the camera, drag and drop the object that should be followed as the target and set the offset to the fixed distance between the camera and the object.

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 WalidMohamed · Aug 14, 2016 at 01:44 PM 0
Share

thank u ^ ^

here is a slight change to adjust the position of the camera

using UnityEngine; using System.Collections;

public class CameraFollow : $$anonymous$$onoBehaviour {

 public Transform thePlayer;

 private Vector3 offset;

 void Start() {
     offset = transform.position - thePlayer.position; 
 }

 void Update() {
     Vector3 cameraNewPosition = new Vector3(thePlayer.position.x + offset.x, offset.y, thePlayer.position.z + offset.z);

     transform.position = cameraNewPosition; 
 }

}

avatar image
0

Answer by DevDavisC · Aug 13, 2016 at 07:30 PM

This should work fine

 public class CameraFollow : MonoBehaviour {
 
     private GameObject player;
     //public float cameraSpeed = 5.0f;
 
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag ("Player");
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         //X position follow
         Vector3 camPos = transform.position;
         camPos.x = player.transform.position.x;
         //camPos.z = player.transform.position.z; // only use if you want to follow from the player's z coordinate
         transform.position = Vector3.Lerp (transform.position, camPos, 15 * Time.fixedDeltaTime);
     }
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 WalidMohamed · Aug 14, 2016 at 01:44 PM 0
Share

thanks ^ ^

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding the limits of transform coordinates 1 Answer

Changeing Camera Position 1 Answer

How to make custom handles take gameobject position offset into account? 0 Answers

Set Game Object Position to Sides of a Camera 1 Answer

Programatically calculate child offset in world space 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges