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 itos · Aug 28, 2013 at 12:59 AM · c#camera2d

Camera Smooth Follow 2D from JS to C#

I am making a script to be used on the Main Camera object so the camera follows the character in a 2D platformer world.

I tried to translate this from a Javascript script to c#, I am getting an error in line 26: "Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable."

Original Javascript Version

 var cameraTarget : GameObject;
 var player : GameObject;
 
 var smoothTime : float = 0,1;
 var cameraFollowX : boolean = true;
 var cameraFollowY : boolean = true;
 var cameraFollowHeight : boolean = false;
 var cameraHeight : float = 2.5;
 var velocity : Vector2;
 private var thisTransform : Transform;
 
 function Start ()
 {
 thisTransform = transform;
 }
 
 function Update () 
 {

 if (cameraFollowX)
 {
 thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
 }

 if (cameraFollowY)
 {
 thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
 }

 if (!cameraFollowX & cameraFollowHeight)
 {
 camera.transform.position.y = cameraHeight;
 }

 }

My C# Version

 using UnityEngine;
 using System.Collections;
 
 public class CameraSmoothFollow : MonoBehaviour {
 
     public GameObject cameraTarget; // object to look at or follow
     public GameObject player; // player object for moving
     
     public float smoothTime = 0.1f;    // time for dampen
     public bool cameraFollowX = true; // camera follows on horizontal
     public bool cameraFollowY = true; // camera follows on vertical
     public bool cameraFollowHeight = true; // camera follow CameraTarget object height
     public float cameraHeight = 2.5f; // height of camera adjustable
     public Vector2 velocity; // speed of camera movement
     
     private Transform thisTransform; // camera Transform
         
     // Use this for initialization
     void Start () {
         thisTransform = transform;
     }
     
     // Update is called once per frame
     void Update () {
         if (cameraFollowX){
             thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime); // Here i get the error
         }
         if (cameraFollowY) {
         // to do    
         }
         if (!cameraFollowX & cameraFollowHeight) {
         // to do
         }
     }
 }


Any help will be appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Jona-Marklund · Aug 28, 2013 at 01:05 AM

Hello,

 using UnityEngine;
 using System.Collections;
 
 public class CameraSmoothFollow : MonoBehaviour
 {
 
     public GameObject cameraTarget; // object to look at or follow
     public GameObject player; // player object for moving
 
     public float smoothTime = 0.1f;    // time for dampen
     public bool cameraFollowX = true; // camera follows on horizontal
     public bool cameraFollowY = true; // camera follows on vertical
     public bool cameraFollowHeight = true; // camera follow CameraTarget object height
     public float cameraHeight = 2.5f; // height of camera adjustable
     public Vector2 velocity; // speed of camera movement
 
     private Transform thisTransform; // camera Transform
 
     // Use this for initialization
     void Start()
     {
         thisTransform = transform;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (cameraFollowX)
         {
             thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
         }
         if (cameraFollowY)
         {
             // to do  
         }
         if (!cameraFollowX & cameraFollowHeight)
         {
             // to do
         }
     }
 }

JS does some under the hood magic which C# doesn't.

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 itos · Sep 02, 2013 at 11:28 PM

Thanks! Researching a little further I learn that Transform.position is a struct and when I was trying to access the X property of position I was trying to access the copy and not the real one. This change have fixed my problem and now the camera scrolls with the player as a target.

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Choppy camera follow. 1 Answer

2D camera zoom smoothing and limitations? 1 Answer

The New 2D Pixel Perfect Camera Package "Stretch Fill" Causes Major Lag 0 Answers

Change the background color attribute of a camera in C#? 2 Answers


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