How to convert this to C#

can anyone help me convert this from JS to C#?

if(target != null){
var dir = target.transform.position - transform.position;
dir = dir.normalized;
rigidbody.AddForce(dir * speed*200 * Time.deltaTime);
}

thanks

The code you posted will work fine in C# without change. Personally I’d change ‘var’ to ‘Vector3’, but that is just because I like everything explicitly typed when I code in C#.

Change var dir to Vector3 dir.

if(target != null){
Vector3 dir = target.transform.position - transform.position;
dir = dir.normalized;
rigidbody.AddForce(dir * speed*200 * Time.deltaTime);
}

Except for that var everything is fine in that code.Even the var can be used on C# but it is not recomended as it is slow.

There is nothing wrong with using the var keyword, but if you do use it a lot then make sure the name of the variable is clear as to it’s use. It would be better not to use it for everything though…