- Home /
shoot game - 2 problems
Hi there ... I'm trying to make a shooter game but I have 2 problems
1- I'm using this code for shooting :
var BulletPerfab : Rigidbody; var speed = 50; var FireSpeed : float = 0.2; private var NextFire : float = 0.0;
function Update () { if (Input.GetButton("Fire1") && Time.time > NextFire) { // create a new projectile, use the same position and rotation as the Launcher. var new_bullet : Rigidbody = Instantiate(BulletPerfab, transform.position, transform.rotation );
// Vector3.X = speed , becuse the Global rotation of the player is (X = forward) , some time we put it Z that depeandes to the Global rotation
new_bullet.velocity = transform.TransformDirection( Vector3( speed , 0 , 0 ) );
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision( new_bullet.collider, transform.root.collider );
NextFire = Time.time + FireSpeed;
}
}
but the problem that the bullet need time to arrive to the wall , and if I increase the speed time of the bullet it's cross the wall with out make any collision, so is there a way to directly put the bullet on the wall, like OnMouseDown on the wall or Enemy the effect directly will show ?
2- how can I let the bullet when I shoot it ,go to the position(X,Y)/Direction of the shoot pointer(GUI). (The GUI it's not on the middle of the screen).
thanks....
Answer by Statement · Mar 29, 2011 at 02:21 PM
- Use Continous Physics or Raycast instead.
- Create a ray from the mouse position or another screen point and get the direction of the ray.
Something like this...
var prefab : Rigidbody;
function Update () { if (Input.GetButtonDown ("Fire1")) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var instance : Rigidbody = Instantiate (prefab, transform.position, transform.rotation); instance.AddForce (ray.direction * 100); } }
I would also recommend to use a raycast. Direct hit weapons don't need bullets ;)
thank u .. but , I'm new in Unity so can u put a code about that
Your answer
Follow this Question
Related Questions
Bullet Effect 2 Answers
Destroy Projectile on collision 3 Answers
Why won't my bullets detect a collision and be destroyed?... 4 Answers
RayCast Problem : 2d 1 Answer
Bullet Shoot with RayCast don't destroy onCollisionEnter 1 Answer