Javascript to C# (89935)

Hello unity community, (RHYMES!)

So I tried to translate a javascript script to a C# script and it gives me an error and I tried looking it up, even came up with some imaginary solutions but nothing worked so far..

using UnityEngine;
using System.Collections;

public class CamFollow : MonoBehaviour {
	private Transform camfollow;
	public float camSpeed = 0.3f;
	private Transform thisTransform;
	private Vector3 velocity;
	// Use this for initialization
	void Start () {
		thisTransform = transform;
	}
	
	// Update is called once per frame
	void Update () {
		thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, camfollow.position.x, 
		                                             ref velocity.x, camSpeed);
		thisTransform.position.z = Mathf.SmoothDamp (thisTransform.position.z, camfollow.position.z, 
		                                             ref velocity.z, camSpeed);
	}
}

Here’s the code and here’s the error

`UnityEngine.Transform.position’. Consider storing the value in a temporary variable

Thanks in advance :slight_smile:
PS: I’m new to this so be easy on me, an explanation would be great aswell

1 Answer

1

This is a common question and searching would have turned up a number of answers. Here is how to make C# happy:

using UnityEngine;
using System.Collections;

public class CamFollow : MonoBehaviour {
	private Transform camfollow;
	public float camSpeed = 0.3f;
	private Transform thisTransform;
	private Vector3 velocity;
	// Use this for initialization
	void Start () {
		thisTransform = transform;
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 v  = thisTransform.position;
		v.x = Mathf.SmoothDamp (v.x, camfollow.position.x, 
		                                             ref velocity.x, camSpeed);
		v.z = Mathf.SmoothDamp (v.z, camfollow.position.z, 
		                                             ref velocity.z, camSpeed);
		thisTransform.position = v;
	}
}