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 Nandha · Nov 30, 2020 at 10:30 AM · scripting problemspawning problems

Hello Friends, When I am trying to shoot, my bullet only goes on one side. Even I turn my Player my bullet doesn't turn, it's only move on one direction. I dont know What to do? Please help Me?

My scripts given below for bulletmove and spawningbullet`using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections.Generic;

public class ProjectileMoveScript : MonoBehaviour {

 public float speed;
 [Tooltip("From 0% to 100%")]
 public float accuracy;
 public float fireRate;
 public GameObject muzzlePrefab;
 public GameObject hitPrefab;
 public AudioClip shotSFX;
 public AudioClip hitSFX;
 public List<GameObject> trails;

 private float speedRandomness;
 private Vector3 offset;
 private bool collided;

 void Start () {    

     if (accuracy != 100) {
         accuracy = 1 - (accuracy / 100);

         for (int i = 0; i < 2; i++) {
             var val = 1 * Random.Range (-accuracy, accuracy);
             var index = Random.Range (0, 2);
             if (i == 0) {
                 if (index == 0)
                     offset = new Vector3 (0, -val, 0);
                 else
                     offset = new Vector3 (0, val, 0);
             } else {
                 if (index == 0)
                     offset = new Vector3 (0, offset.y, -val);
                 else
                     offset = new Vector3 (0, offset.y, val);
             }
         }
     }

     if (muzzlePrefab != null) {
         var muzzleVFX = Instantiate (muzzlePrefab, transform.position, Quaternion.identity);
         muzzleVFX.transform.forward = gameObject.transform.forward + offset;
         var ps = muzzleVFX.GetComponent<ParticleSystem>();
         if (ps != null)
             Destroy (muzzleVFX, ps.main.duration);
         else {
             var psChild = muzzleVFX.transform.GetChild(0).GetComponent<ParticleSystem>();
             Destroy (muzzleVFX, psChild.main.duration);
         }
     }

     if (shotSFX != null && GetComponent<AudioSource>()) {
         GetComponent<AudioSource> ().PlayOneShot (shotSFX);
     }
 }

 void Update () {    
     if (speed != 0)
         transform.position += (transform.forward + offset) * (speed * Time.deltaTime);
 }

 void OnCollisionEnter (Collision co) {
     if (co.gameObject.tag != "Bullet" && !collided) {
         collided = true;
         
         if (shotSFX != null && GetComponent<AudioSource>()) {
             GetComponent<AudioSource> ().PlayOneShot (hitSFX);
         }

         if (trails.Count > 0) {
             for (int i = 0; i < trails.Count; i++) {
                 trails [i].transform.parent = null;
                 var ps = trails [i].GetComponent<ParticleSystem> ();
                 if (ps != null) {
                     ps.Stop ();
                     Destroy (ps.gameObject, ps.main.duration + ps.main.startLifetime.constantMax);
                 }
             }
         }
     
         speed = 0;
         GetComponent<Rigidbody> ().isKinematic = true;

         ContactPoint contact = co.contacts [0];
         Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal);
         Vector3 pos = contact.point;

         if (hitPrefab != null) {
             var hitVFX = Instantiate (hitPrefab, pos, rot);
             var ps = hitVFX.GetComponent<ParticleSystem> ();
             if (ps == null) {
                 var psChild = hitVFX.transform.GetChild (0).GetComponent<ParticleSystem> ();
                 Destroy (hitVFX, psChild.main.duration);
             } else
                 Destroy (hitVFX, ps.main.duration);
         }

         StartCoroutine (DestroyParticle (0f));
     }
 }

 public IEnumerator DestroyParticle (float waitTime) {

     if (transform.childCount > 0 && waitTime != 0) {
         List<Transform> tList = new List<Transform> ();

         foreach (Transform t in transform.GetChild(0).transform) {
             tList.Add (t);
         }        

         while (transform.GetChild(0).localScale.x > 0) {
             yield return new WaitForSeconds (0.01f);
             transform.GetChild(0).localScale -= new Vector3 (0.1f, 0.1f, 0.1f);
             for (int i = 0; i < tList.Count; i++) {
                 tList[i].localScale -= new Vector3 (0.1f, 0.1f, 0.1f);
             }
         }
     }
     
     yield return new WaitForSeconds (waitTime);
     Destroy (gameObject);
 }


}

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class SpawnProjectilesScript : MonoBehaviour {
 
     
     public GameObject firePoint;
     public List<GameObject> VFXs = new List<GameObject> ();
 
     private float timeToFire = 0f;
     private GameObject effectToSpawn;
 
     void Start () {
         
         Application.targetFrameRate = 60;
         effectToSpawn = VFXs[0];
     }
 
     void Update () {
         if (Input.GetKey (KeyCode.Space) && Time.time >= timeToFire || Input.GetMouseButton (0) && Time.time >= timeToFire) {
             timeToFire = Time.time + 1f / effectToSpawn.GetComponent<ProjectileMoveScript>().fireRate;
             SpawnVFX ();    
         }
 
     }
 
     public void SpawnVFX () {
         GameObject vfx;
 
         if (firePoint != null) {
             vfx = Instantiate (effectToSpawn, firePoint.transform.position, Quaternion.identity);
         }
         else
             vfx = Instantiate (effectToSpawn);
 
         var ps = vfx.GetComponent<ParticleSystem> ();
 
         if (vfx.transform.childCount > 0) {
             ps = vfx.transform.GetChild (0).GetComponent<ParticleSystem> ();
         }
     }
 
 }
 


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

0 Replies

· Add your reply
  • Sort: 

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

252 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

Related Questions

Problems with instantiate 0 Answers

Spawn meshes to scene via dropdown UI button 2 Answers

Instantiate not working? 0 Answers

FPS Bullet spawn problem 1 Answer

My character spawns in spawn point then instantly teleports to random location over and over again. 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