- Home /
Change script from 3d to 2d Game.
Hello everyone.
Currently I have a script Which fires projectiles That works well in 3D.
I need help to change it in 2d shot. I added Rigidbody2D. By testing the script works, but the shot does not travel on the axis x or -x. the shot explodes in front of the character in 2D without traveling on the axis X. How can you change this script from 3d to 2d shot?
#pragma strict
var Prefab : Transform;
var PrefabSpeed : float = 6000;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(Collisions.GRENADE_AMMO > 0)
if(!Prefab || !PrefabSpeed)
{
Debug.Log("Il prefab o la velocità non esiste");
}
else
{
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.AddForce(transform.forward * PrefabSpeed);
Collisions.GRENADE_AMMO --;
GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_AMMO;
print("YOU NOW HAVE " + Collisions.GRENADE_AMMO + " GRENADES");
}
}
}
Answer by Fappp · May 09, 2015 at 09:02 AM
Use this:
PrefabCreate.rigidbody2D.AddForce(transform.forward * PrefabSpeed, ForceMode2D.Impulse);
Thanks for the reply.
I tried this,PrefabCreate.rigidbody2D.AddForce (transform.forward * PrefabSpeed, Force$$anonymous$$ode2D.Impulse ); yet but the shot remains stationary.
Yes. the rigidbody is kinematic. Now when the character jumps on the normal shot. But when idle, the shot explodes. the problem should be in the Projectile Spawn, vove part by the blow.
I moved the Projectile Spawn, at the top is now working. But itself I move too far down the shot explodes. I do not understand the problem. :-(
Answer by Davrinsky-sky · Jul 06, 2017 at 12:40 AM
I'm wondering if you are shooting your projectile in the axis that is parallel to your main camera when you need perpendicular if the projectile is behind your 2D character. Try shooting out on the axis that is perpendicular to your main camera view. I don't know if you want to rearrange your whole scene on a different axis or have your script change the projectiles to go along a different axis when in 3D.
just an offhand thought.
Answer by Jwizard93 · Jul 05, 2017 at 10:01 PM
Are you sure that transform.forward is the x-Axis? It should probably be transform.right. Also I suggest you clean this up. If it were any more complex than it is I wouldn't have read it for its messiness.
Your answer
Follow this Question
Related Questions
Shooting 2D - how to fix 1 Answer
Changing prefabs to 2D images 0 Answers
throw / fling object or prefab 1 Answer
Is is possible to replace a prefab with a variant in script? 1 Answer