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 /
  • Help Room /
avatar image
0
Question by KingSloth · May 31, 2017 at 12:16 AM · c#movementshootingmovement scriptproblema

[I still need help even though there is 2 replies] Help with shooter script

Hi! I just need help with my character shooting script. Right now the bullet spawns somewhere other than from the character's gun. What do I do to fix that. Here are my scripts: Shoot script attached to the player:

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

public class Shoot : MonoBehaviour {

 public GameObject projectilePrefab;
 GameObject projectileInstance;
 public float offset;
 public float offsetTwo;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.V)) {
         Instantiate (projectilePrefab, (transform.position * offsetTwo) + (Vector3.right * offset), transform.rotation);
     }


 }

}

Script moving the bullet:

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

public class ProjectileMove : MonoBehaviour {

 public float speed1 = 100;


 // Use this for initialization
 void move () {
     transform.Translate (Vector3.left * Time.deltaTime * speed1);
 }
 
 // Update is called once per frame
 void Update () {
     move ();
 }

}

. And Yes! I have tried changing the offsets, many times.

Comment
Add comment · Show 1
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 ForeignGod · May 31, 2017 at 12:26 AM 1
Share

You already have 2 offset variables, have you tried changing them untill the projectile spawns infront of your gun?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Skaster87 · May 31, 2017 at 12:39 AM

Hi @KingSloth

The first problem I see is in the Update() function when you instantiate you use two offset, and you're multiplying the Vectors inside the parenthesis, was that intentional?

What I would do instead is declare a variable that contains the location you want to spawn at. So on your Gun create an empty GameObject and place the transform at the tip of the gun. In your first script add public Transform firePoint; then in the inspector drag the GameObject into the slot. After that you can change your instantiate to Instantiate (projectilePrefab, firePoint.position, firePoint.rotation);

Good Luck!

 using UnityEngine;
 
 public class Shoot : MonoBehaviour {
 
  public GameObject projectilePrefab;
  GameObject projectileInstance;
  public Transform firePoint;
  // Use this for initialization
  void Start () {
      
  }
  
  // Update is called once per frame
  void Update () {
      if (Input.GetKeyDown(KeyCode.V)) {
          Instantiate (projectilePrefab, firePoint.poistion, firePoint.rotation);
      }
  }
 }

alt text


capture.jpg (179.8 kB)
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
avatar image
0

Answer by KingSloth · May 31, 2017 at 12:41 AM

@Skaster87 The bullet is being spawned really far away from fire point.

Comment
Add comment · Show 3 · 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 Skaster87 · May 31, 2017 at 01:59 AM 0
Share

If your certain the firepoint transform is in the correct location, and you dragged it correctly into the script, then I think its your speed variable. Try bringing it down to 1 ins$$anonymous$$d of 100.

avatar image bobisgod234 · May 31, 2017 at 07:20 AM 0
Share

Did you try Skaster87's suggestion to use a separate transform for the bullet origin, and remove the offset vectors? If so, try setting the speed to 0 and verify in scene view that the bullets are being instantiated at the same position as the transform you assigned to the Shoot script.

Also, make sure not to ask questions or post comments as answers. Ins$$anonymous$$d, click "Add comment" to the answer you wish to comment on.

avatar image KingSloth · Jun 01, 2017 at 11:54 PM 0
Share

Hey guys, thank you so much for your help, I am going to test it right now. The reason I used the reply, is that when I pressed "add comment" yesterday, It just refreshed the page. It is working now though! Thanks again.

avatar image
0

Answer by Ark_Revan · May 31, 2017 at 07:33 AM

You need to create an empty game object that will be a child of your gun. Then you put this empty game object in front of the gun, where you want it to shoot. Be careful not to put it inside or your bullet will collide with the gun. Rotate it in the same direction as the gun ( you don't want the bullet to do an angle when you shoot , don't you ?)

Then you add your shooting script to this empty game object. This script need 3 things: 1) A public gameobject that will be used to store your bullet prefab ( there are other way but it's this easiest I can think of ). Take care that this prefab is well done ( especially, reset the position so the object is in the center of ... itself ? not sure how to explain it. but you don't want the bullet to have an offset when it appears. 2) The trigger to fire ( here, your mouse click ) 3) A function to instantiate your bullet. Since you put the script in an empty game object which is near enough from the gun. you only need to instantiate it with the transform.position & the transform.rotation without any cast/call or anything. Then you should give it a velocity along the forward axis.

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

330 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Switching lanes 1 Answer

Isometric movement for MoveTowards 0 Answers

Why is my bool being set to true when program is run? 1 Answer

How to have a child object camera with gaze move its parent object 0 Answers

Movement input problem? 1 Answer


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