Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GamingOink · Mar 16, 2017 at 04:02 PM · raycastprojectileshooter

Need urgent help to create projectile shooting script with raycasting

I want to shoot projectiles randomly from one of modules every 1-2 seconds (time between bullets decreases as game goes on). The objective of the game is to have the player dodge the bullets. I have mainly finished my player movement script, but I am stuck on how to get the projectiles to shoot straight (currently they just fly up into the air turning and twisting) and from different positions (each position will represent a different turret firing - ex: (1,0,10) is position of turret one and (5, 0, 10) is position of second turret). Right now the bullets fire very fast and flying all over the place instead in a straight line. I was thinking of using raycasting to detect if the player has been hit. Also I was thinking of using raycasting to measure the distance of the bullet from the turret so the script knows when to fire the next bullet (I only want one bullet in the scene at a time). Currently I have a random number generator to pick which module (basically position) to fire the bullet from. Here is my code (very incomplete -- just looking for some tips and basic structure):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class LaserShooter : MonoBehaviour {
 
 private int modChoose;
 public GameObject projectile;
     //Defining variables...
 void Start () {
     modChoose = Random.Range (1, 4);
     //modChoose is picking which module to shoot from. In this case, the module is basically a position where the bullet should start.
 }

 void Update () {
     if (modChoose == 1) {
     GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
     bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10);
     }
     //If the first module is picked, fire a bullet at a specific start pos (still havent added the start pos script though) etc.
     if (modChoose == 2) {
     GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
     bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10);
     }
     if (modChoose == 3) {
     GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
     bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10);
     }
 }
                 
 } 

Thanks for help :)

Comment
Add comment · Show 2
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 Deathdefy · Mar 16, 2017 at 04:25 PM 0
Share

Right now you just have the code copy + pasted into each mod. I would suggest removing your random selection for now and just get 1 working. As of now your projectile is going to spawn wherever your LaserShooter script is in the scene and not at a different location based on separate positions.

avatar image GamingOink · Mar 16, 2017 at 06:11 PM 0
Share

Yes, but how come the projectiles are flying all over the place?

1 Reply

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

Answer by GamingOink · Mar 23, 2017 at 08:10 AM

You can euler to rotate the projectile at spawn

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fire Raycast Toward Current Mouse Position 1 Answer

Projectile Hit Detection with Bullet Drop 1 Answer

how to take health/hearth from player when colliding with an object? 2 Answers

Handling Projectiles in a Multiplayer Environment 0 Answers

How to Implement First Shot Accuracy? 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