what is c# code of this java code:
var projectile : Rigidbody;
var speed = 20;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
}
}
PhilipV
February 17, 2010, 5:18pm
2
using UnityEngine;
using System.Collections;
public class Myclass : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 20.0f;
private Transform myTransform;
void Start()
{
myTransform = this.transform;
}
void Update ()
{
if ( Input.GetButtonDown ("Fire1") )
{
Rigidbody instantiatedProjectile = Instantiate ( projectile, myTransform.position, myTransform.rotation ) as Rigidbody;
instantiatedProjectile.velocity = myTransform.TransformDirection( 0,0, speed );
Physics.IgnoreCollision( instantiatedProjectile.collider, myTransform.root.collider );
}
}
}
Added a cached myTransform variable for faster access.
Cheers
Philip
so thanks my friend.
can you discuss more about that Cash that you saied?
Cheers,
thanks,
by the way,the code does not work correctly.can u test it?
thanks.
jashan
February 17, 2010, 6:14pm
5
Just to avoid any confusion: The script you posted above is not Java. It’s UnityScript, or JavaScript if you like. Java is a language that is quite different from JavaScript and much more like C#.
The only reason JavaScript was called JavaScript back then was because Java was about to be the Web language (with Java applets, if anyone remembers). Ironically, Java is hardly used on the Web now (at least for the client-side), while JavaScript has become the Web-scripting language.
PhilipV
February 17, 2010, 8:23pm
6
Hmm, I always test my code, and this is no difference, otherwise i’d post a notice…
Anyway, I tested this code and it is behaving exactly as intended, throwing an object forward of the parent object, with the desired speed…
Obviously you need to implement this script inside a script called “Myclass”, or just grab out the pieces you need.
About “Cache” (Different from Cash ): when you type
transform.position = Vector3 (something);
you are actually calling ( it is hidden from you, but if you check CIL code generated by Unity, you will see that this is what is written there )
GetComponent(typeof(Transform)).position = Vecto3 (something);
Since the result of GetComponent is always the exact same Transform component, i Cache it, or store it, in a variable, that is assigned just one time.
Philip
thanks my friend.the was correct.i was incorrect.
what is C# of this code please:
for(var hit in colliders)
thx
PhilipV
February 18, 2010, 6:01pm
9
Untested, but should be
for each ( RaycastHit hit in colliders )
(Just guessing from your code the type of the hit var, obviously you should adjust it if my guess is wrong.
Philip