Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Iceblitzyt · Nov 02, 2013 at 04:10 PM · c#shootingbulletship

Issue Firing lazer(bullet) in space game. c#

Hey there,

So I have a simple script which is meant to fire a lazer at the point I have chosen. The issue I'm having is each time I fire a lazer not only is it not moving forward in the direction i want it to, it's spinning my ship round and round in circles.

This is the Lazer Shooting script:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponFire : MonoBehaviour {
     public Rigidbody Lazer;
     private float LazerSpeed = 500;
     
     
 
     void Update () {
         
     if(Input.GetKeyDown(KeyCode.Alpha1)){
             
     Rigidbody FireLazer = Instantiate(Lazer, transform.position,transform.rotation) as Rigidbody;
             
     FireLazer.AddForce(transform.forward * LazerSpeed);
             
         }
     }
 }
 

As you can see it's very simple. This is my ship movement script, also simple:

 using UnityEngine;
 using System.Collections;
 
 public class ShipMovement : MonoBehaviour {
     public float Movementspeed;
     public float FTLDrive = 2000;
     
     
     void Update () {
         
         this.transform.position += this.transform.right * Movementspeed * Time.deltaTime * Input.GetAxis("Vertical");
         if(Input.GetKey(KeyCode.A)){
         this.transform.Rotate(Vector3.down, 90 * Time.deltaTime);    
         }
         if(Input.GetKey(KeyCode.D)){
         this.transform.Rotate(Vector3.up, 90 * Time.deltaTime);    
         }
     if(PlayerPrefs.GetInt("UnlockHyperDrive",0)== 2){    
         if(Input.GetKeyDown(KeyCode.LeftShift)){
         Movementspeed = FTLDrive;    
         }
         else if(Input.GetKeyUp(KeyCode.LeftShift)){
         Movementspeed = 100;
         }
         }
         
         
         
     }
 }
 

I have a rigidbody on my ship and have attached one to the lazer prefab. The lazer point where i want the lazer to spawn and fire is also a child of the ship. Can someone help me correct this issue please?

Many thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Tomer-Barkan · Nov 03, 2013 at 04:45 AM

When two rigidbodies collide with each other, there is a collision resolution algorithm that calculates and applies forces on both objects. This is part of Unity's physics engine, and that is probably what causes your ship to spin.

There is a solution. There is a method called Physics.IgnoreCollision, which tells unity not to calculate collisions between two specific objects. There are other ways, such as setting the layer collision matrix, or disabling one of the rigidbodies and making one of the colliders a trigger, but Physics.IgnoreCollision is the simplest in my opinion for this case.

 if(Input.GetKeyDown(KeyCode.Alpha1)){
     Rigidbody FireLazer = Instantiate(Lazer, transform.position,transform.rotation) as Rigidbody;
     Physics.IgnoreCollision(this.collider, FireLaser.collider);
     FireLazer.AddForce(transform.forward * LazerSpeed);
 }

Edit: If your bullet still doesn't move forward like you expect, share its code so we can help with it as well.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Iceblitzyt · Nov 03, 2013 at 06:29 PM 0
Share

Excellent, this worked like a dream :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Sync bullet spawn with shoot animation 0 Answers

Shooting script C# 1 Answer

How to make 3 bullets fire at different angles 1 Answer

2d bullet shot at bullet emitter 0 Answers

Shot Delay between button press. c# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges