📜 ⬆️ ⬇️

Unity3D + C #, or how to translate scripts

image

I came across Unity3D relatively recently, before that I had worked with my own handwriting engines for Java2ME mobile phones, the rest of the time I’m a dotnetchik.

When switching to a new platform, I first of all looked for a ready-made technology platform for myself, and the main criteria for me were the price (affordable / reasonable) and, if possible, multiplatform, so that once written code could be used again and again without conversion. Almost immediately, I stumbled upon Unity.
')
I have already written about Unity as a whole here, so I won’t repeat, the main thing is that you can (and in my opinion, develop the game) the script in C # (thanks to Mono ). The only restriction is to write within the .NET Framework 1.1 - only it is supported on the iPhone. [upd: while I was writing this post, a new version of Unity3D for iPhone was released that supports .NET 2.1]

So, key advantages for me from using Unity in general and C # in particular:

Of course, there are also disadvantages, but it is not for them, because the advantages ultimately outweighed.

Scripting is based on the UniSciTE based Scintilla editor, which I personally didn’t like, and therefore I immediately wanted to use the good old Visual Studio. Here's how to script in C # will be discussed below:

[upd: transferred to Game Development]



The current version of Unity is 2.6.1, and there are no problems with it with integration with Visual Studio - just right-click in the project browser window and select the appropriate menu item. After that, Unity will create a solution, and if it has already been created, it will update it. The project will already contain links to the used UnityEditor and UnityEngine libraries, and all files with scripts (regardless of language) will be added to the project.
VisualStudio

What to keep in mind:


To describe other subtleties, I’ll come with an example of a simple script that comes with the engine and allows you to move as in FPS, that is, wasd + space for a jump + mouse. To prevent the user's avatar from falling below the floor, it must be wrapped into a simple collider, the CharacterController, which should stick to the game object automatically when the FPSWalker entity is applied to it.

// <br/>
var speed = 6.0 ; <br/>
var jumpSpeed = 8.0 ; <br/>
var gravity = 20.0 ; <br/>
<br/>
// <br/>
private var moveDirection = Vector3. zero ; <br/>
private var grounded : boolean = false ; <br/>
<br/>
// <br/>
function FixedUpdate ( ) { <br/>
// <br/>
if ( grounded ) { <br/>
// <br/>
moveDirection = new Vector3 ( Input. GetAxis ( "Horizontal" ) , 0 , Input. GetAxis ( "Vertical" ) ) ; <br/>
// <br/>
moveDirection = transform. TransformDirection ( moveDirection ) ; <br/>
// <br/>
moveDirection *= speed ; <br/>
<br/>
// - <br/>
if ( Input. GetButton ( "Jump" ) ) { <br/>
moveDirection. y = jumpSpeed ; <br/>
} <br/>
} <br/>
<br/>
// <br/>
moveDirection. y -= gravity * Time. deltaTime ; <br/>
<br/>
// <br/>
var controller : CharacterController = GetComponent ( CharacterController ) ; <br/>
// <br/>
var flags = controller. Move ( moveDirection * Time. deltaTime ) ; <br/>
// , <br/>
grounded = ( flags & CollisionFlags. CollidedBelow ) != 0 ; <br/>
} <br/>
// , , CharacterController <br/>
@ script RequireComponent ( CharacterController )
// <br/>
var speed = 6.0 ; <br/>
var jumpSpeed = 8.0 ; <br/>
var gravity = 20.0 ; <br/>
<br/>
// <br/>
private var moveDirection = Vector3. zero ; <br/>
private var grounded : boolean = false ; <br/>
<br/>
// <br/>
function FixedUpdate ( ) { <br/>
// <br/>
if ( grounded ) { <br/>
// <br/>
moveDirection = new Vector3 ( Input. GetAxis ( "Horizontal" ) , 0 , Input. GetAxis ( "Vertical" ) ) ; <br/>
// <br/>
moveDirection = transform. TransformDirection ( moveDirection ) ; <br/>
// <br/>
moveDirection *= speed ; <br/>
<br/>
// - <br/>
if ( Input. GetButton ( "Jump" ) ) { <br/>
moveDirection. y = jumpSpeed ; <br/>
} <br/>
} <br/>
<br/>
// <br/>
moveDirection. y -= gravity * Time. deltaTime ; <br/>
<br/>
// <br/>
var controller : CharacterController = GetComponent ( CharacterController ) ; <br/>
// <br/>
var flags = controller. Move ( moveDirection * Time. deltaTime ) ; <br/>
// , <br/>
grounded = ( flags & CollisionFlags. CollidedBelow ) != 0 ; <br/>
} <br/>
// , , CharacterController <br/>
@ script RequireComponent ( CharacterController )

So, when translating this script into C #, in addition to the above, you should consider:

After applying all these simple rules, our class looks like this:

using UnityEngine ; <br/>
<br/>
// <br/>
[ RequireComponent ( typeof ( CharacterController ) ) ] <br/>
// <br/>
public class FPSWalkerInCS : MonoBehaviour { <br/>
// , float <br/>
public float speed = 6.0f ; <br/>
public float jumpSpeed = 8.0f ; <br/>
public float gravity = 20.0f ; <br/>
// - <br/>
private Vector3 moveDirection = Vector3. zero ; <br/>
private bool grounded = false ; <br/>
<br/>
// <br/>
void Start ( ) { <br/>
<br/>
} <br/>
<br/>
void FixedUpdate ( ) { <br/>
if ( grounded ) { <br/>
moveDirection = new Vector3 ( Input. GetAxis ( "Horizontal" ) , 0 , Input. GetAxis ( "Vertical" ) ) ; <br/>
moveDirection = transform. TransformDirection ( moveDirection ) ; <br/>
moveDirection *= speed ; <br/>
<br/>
if ( Input. GetButton ( "Jump" ) ) { <br/>
moveDirection. y = jumpSpeed ; <br/>
} <br/>
} <br/>
<br/>
moveDirection. y -= gravity * Time. deltaTime ; <br/>
// <br/>
CharacterController controller = ( CharacterController ) GetComponent ( typeof ( CharacterController ) ) ; <br/>
CollisionFlags flags = controller. Move ( moveDirection * Time. deltaTime ) ; <br/>
grounded = ( flags & CollisionFlags. CollidedBelow ) != 0 ; <br/>
} <br/>
}
using UnityEngine ; <br/>
<br/>
// <br/>
[ RequireComponent ( typeof ( CharacterController ) ) ] <br/>
// <br/>
public class FPSWalkerInCS : MonoBehaviour { <br/>
// , float <br/>
public float speed = 6.0f ; <br/>
public float jumpSpeed = 8.0f ; <br/>
public float gravity = 20.0f ; <br/>
// - <br/>
private Vector3 moveDirection = Vector3. zero ; <br/>
private bool grounded = false ; <br/>
<br/>
// <br/>
void Start ( ) { <br/>
<br/>
} <br/>
<br/>
void FixedUpdate ( ) { <br/>
if ( grounded ) { <br/>
moveDirection = new Vector3 ( Input. GetAxis ( "Horizontal" ) , 0 , Input. GetAxis ( "Vertical" ) ) ; <br/>
moveDirection = transform. TransformDirection ( moveDirection ) ; <br/>
moveDirection *= speed ; <br/>
<br/>
if ( Input. GetButton ( "Jump" ) ) { <br/>
moveDirection. y = jumpSpeed ; <br/>
} <br/>
} <br/>
<br/>
moveDirection. y -= gravity * Time. deltaTime ; <br/>
// <br/>
CharacterController controller = ( CharacterController ) GetComponent ( typeof ( CharacterController ) ) ; <br/>
CollisionFlags flags = controller. Move ( moveDirection * Time. deltaTime ) ; <br/>
grounded = ( flags & CollisionFlags. CollidedBelow ) != 0 ; <br/>
} <br/>
}

All class is ready.

Source: https://habr.com/ru/post/86456/


All Articles