Hello
There is a series of great tutorials on Youtube made by TornadoTwins. http://www.youtube.com/user/TornadoTwins#p/c/11F87EB39F84E292
The tutorials are very good and easy to follow, but sadly they are in Javascript. After having done the 3d Buzz unity c# Tutorials I can’t live without visual studios very helpful intellisence so I tryed to convert the TornadoTwins tutorials to c#. Unfortunately I dont know where to start. It would be nice if someone could help me. I’m sure there are lots of people out there who would like to have a c# translation as well. The TornadoTwins themselves would like to post such a translation but sadly they are not knowing anything about c#.
Here is the first part of the tutorial:
var speed = 3.0;
var rotateSpeed = 3.0;
var bullitPrefab:Transform;
var bullitPrefab2:Transform;
function Update () {
var controller :CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis(“Horizontal”)*rotateSpeed,0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed= speed * Input.GetAxis(“Vertical”);
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown(“Fire1”))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find(“SpawnPoint2”).transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 2000);
}
if(Input.GetButtonDown(“Fire2”))
{
var bullitB = Instantiate(bullitPrefab2, GameObject.Find(“SpawnPoint”).transform.position, Quaternion.identity);
}
if(Input.GetButtonDown(“Jump”))
{
transform.position.y+=10;
}
} @script RequireComponent(CharacterController)
Threre are a lot of other files but one step after the other. Maybe someone could telle some basic guidelines in converting Unity Javascript to c#
You’re right, unfortunately I know very little about C# (err… nothing).
If there’s some C# guru out there willing to help you out, that would indeed be awesome. I know there are lots of people being helped by these tuts (25 000 views a month) so it’s worth the effort.
using UnityEngine;
using System.Collections;
// above are needed to use Unity and most C# library objects
// In C#, RequireComponent has to be before the class definition
[RequireComponent(typeof(CharacterController))]
// below is C# class definition, derived from MonoBehaviour (for Unity Editor)
// not sure what the class name in the example is, so I called it CharacterInput
// Should be saved in a file called CharacterInput.cs (C# class name must match filename)
public class CharacterInput : MonoBehaviour
{
// public members are viewable/editable in the Editor
// C# is strongly typed, so no var, you need to specify type
public float speed = 3.0f; // f suffix is required on numbers of type float
public float rotateSpeed = 3.0f;
public Transform bullitPrefab;
public Transform bullitPrefab2;
// C# member function definition
void Update ()
{
// C# is like C/C++/Java i.e. type name = value
// in C# you need to cast to type, and GetComponent takes a class name string
CharacterController controller = (CharacterController)GetComponent("CharacterController");
transform.Rotate(0f, Input.GetAxis("Horizontal")*rotateSpeed, 0f);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Fire1"))
{
// below is dangerous -- if "SpawnPoint2" isn't found you will get an exception crash -- but I am doing a line-for-line translation to C# here (not a C# issue, same danger in js)
GameObject bullit = (GameObject)Instantiate(bullitPrefab,
GameObject.Find("SpawnPoint2").transform.position, Quaternion.identity);
// again dangerous! if bullit isn't assigned in the previous line, this will exception crash!
bullit.rigidbody.AddForce(transform.forward * 2000f);
}
if(Input.GetButtonDown("Fire2"))
{
GameObject bullitB = (GameObject)Instantiate(bullitPrefab2, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
}
if(Input.GetButtonDown("Jump"))
{
//transform.position.y += 10;
// In C#, you cannot assign to accessor members like the above in js, so instead you can do the following for the same result
transform.position += Vector3.up * 10f;
}
} // end of Update() function definition
} // end of CharacterInput class definition
This builds without error, just a warning that “The variable `bullitB’ is assigned but its value is never used”.
Here ya go [EDIT] Oops, sorry Bren, wasn’t trying to step on your post, I didn’t know you had posted your code, when I was writing mine.
// SimpleFPS.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Rigidbody))]
public class SimpleFPS : MonoBehaviour {
// declare exposed variables
public float speed = 3.0f;
public float rotateSpeed = 3.0f;
public float jumpDistance = 10.0f;
public float bulletForce = 1000.0f;
public GameObject bulletPrefab1;
public GameObject bulletPrefab2;
public Transform bulletSpawnPosition; // this is more efficient than GameObject.Find()
// declare private variables
private GameObject fireBullet;
private CharacterController controller;
private Vector3 forward;
private float curSpeed = 0.0f;
void Start() {
controller = gameObject.GetComponent(typeof(CharacterController)) as CharacterController;
}
void Update() {
// rotating/moving
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
forward = transform.TransformDirection(Vector3.forward);
curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
// firing
if (Input.GetButtonDown("Fire1")) {
fireBullet = (GameObject)Instantiate(bulletPrefab1, bulletSpawnPosition.position, Quaternion.identity);
fireBullet.rigidbody.AddForce(forward * bulletForce);
}
if (Input.GetButtonDown("Fire2")) {
fireBullet = (GameObject)Instantiate(bulletPrefab2, bulletSpawnPosition.position, Quaternion.identity);
fireBullet.rigidbody.AddForce(forward * bulletForce);
}
// jump
if (Input.GetButtonDown("Jump")) {
transform.position = new Vector3(0.0f, transform.position.y + jumpDistance, 0.0f);
}
}
}
Apply this to the bullet, so it destroys after a given amount of time:
// DestroyAfterTime.cs
using UnityEngine;
using System.Collections;
public class DestroyAfterTime : MonoBehaviour {
// declare public variables
public float timeToLive = 2.0f;
void Start() {
Destroy(gameObject, timeToLive);
}
}
Wow, you guys are AWESOME!!!
Thanks so much for helping us out here!!
These are officially the friendliest forums in the world.
Honestly didn’t think anyone would take the time to help out!
Thank you it works and i now know how to translate thinks like this.
here is the full code:
using UnityEngine;
using System.Collections;
public class MonoBehaviour1 : MonoBehaviour
{
#region Functions
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "feind")
{
col.gameObject.renderer.enabled = false;
}
}
}
Here is the code for the turret translated into c#
using UnityEngine;
using System.Collections;
public class turret : MonoBehaviour {
public Transform tg;
public float dramp=5.0f;
public GameObject bullitprefab;
public Transform bulletSpawnPosition;
private GameObject fireBullet;
public int seconds;
private int oddeven;
public int savedtime=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (tg)
{
transform.rotation= Quaternion.Slerp(transform.rotation,(Quaternion.LookRotation(
tg.position - transform.position)),Time.deltaTime * dramp);
seconds=(int)Time.time;
oddeven = (seconds % 2);
if (oddeven==1)
{
shoot(seconds);
}
}
}
void shoot(int seconds)
{
if (seconds != savedtime)
{
(bullitprefab, transform.Find("feindSpawnPoint").transform.position,Quaternion.identity);
fireBullet = (GameObject)Instantiate(bullitprefab, bulletSpawnPosition.position, Quaternion.identity);
fireBullet.rigidbody.AddForce(transform.forward * 1000);
savedtime = seconds;
}
}
}
var bullitPrefab:Transform;
function Update () {
if(Input.GetKeyDown("mouse 2")) {
var bullit2 = Instantiate(bullitPrefab, GameObject.Find("shootPoint").transform.position, Quaternion.identity);
bullit2.rigidbody.AddForce(transform.forward * 2000);
}
}
but C# only spawns the fire ball! it doesn’t move!
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour {
public GameObject bullitPrefab;
private GameObject bullit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("mouse 1")) {
bullit = (GameObject)Instantiate(bullitPrefab, GameObject.Find("shootPoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 300f);
}
}
}
and the error is:
InvalidCastException: Cannot cast from source type to destination type.
Shooting.Update () (at Assets/Scripts/Controlers/Shooting.cs:18)
Hi guys, sorry to add to a now ‘dead’ thread but ive only just started following them and have a problem myself. It’s with the later version of MoveAround
//getting hit
var tumbleSpeed = 800;
var decreaseTime = 0.01;
var decayTime = 0.01;
static var gotHIT = false;
private var backup = [tumbleSpeed, decreaseTime, decayTime];
//function OnControllerColliderHit(hit : ControllerColliderHit)
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "fallout")
{
dead = true;
//subtract life here
HealthControlJS.LIVES -= 1;
HealthControlJS.HITS = 3;
}
if(hit.gameObject.tag == "enemyProjectile")
{
gotHIT = true;
HealthControlJS.HITS += 1;
Destroy(hit.gameObject);
}
}
function LateUpdate()
{
if(dead)
{
transform.position = Vector3(0,1,0);
gameObject.Find("Main Camera").transform.position = Vector3(0,1,-10);
dead = false;
}
if(gotHIT)
{
if(tumbleSpeed < 1)
{
//we are not hit now so reset and get back to playing
//setting everything back to there defualts
tumbleSpeed = backup[0];
decreaseTime = backup[1];
decayTime = backup[2];
gotHIT = false;
}
else
{
//we are hit - lets go for a spin
transform.Rotate(0,tumbleSpeed * Time.deltaTime,0, Space.World); //(x,y,z,(local or global))
tumbleSpeed = tumbleSpeed - decreaseTime;
decreaseTime += decayTime;
}
}
}