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 dragonlord119 · Mar 31, 2015 at 02:29 PM · c#aienemyvelocitybullet

My bullet has no velocity after it is spawned.

I can instantiate my bullet but no code i put after it will give it a velocity. Addforce and transform won't work. I keep getting errors maybe because I'm using Unity 5. Please help me. I can post the whole code if requested but I'm pretty sure all I need is this last line. Also I'm not getting an error as technically the prefab is spawned it just doesn't have any velocity. The prefab for the bullet has a simple destroy gameobject when it collides script.

using UnityEngine;

using System.Collections;

public class EnemyAIScript : MonoBehaviour

{

 public Transform player;
  
 public float playerDistance;
  
 public float rotationDamping;
  
 public float chaseStartRange;
  
 public float moveSpeed;
  
 public static bool isPlayerAlive = true;
  
 public float fireRate;
  
 public float maxFire;
  
 public float nextFire;
  
 public GameObject BulletPrefab;
  
 public float BulletSpeed;
  
 void Update ()
 {
 Vector3 direction = transform.position;
  
 if (isPlayerAlive)
 {
  
 playerDistance = Vector3.Distance (player.position, transform.position);
  
 if (playerDistance < 20f)
 {
  
 lookAtPlayer ();
  
 }
  
 if (playerDistance < 20f)
 {
  
 if (playerDistance > 8f)
 {
  
 chase ();
 }
  
 else if (playerDistance < 8f)
 {
  
 attack ();
  
 }
  
 }
  
 }
  
 }
  
 void lookAtPlayer ()
  
 {
  
 Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
  
 transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
  
 }
  
 void chase ()
  
 {
  
 transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
  
 }
  
 void attack ()
  
 {
  
 if (Time.time > nextFire)
 {
  
 nextFire = Time.time + fireRate;
 Instantiate (BulletPrefab, GameObject.Find ("spawnPoint").transform.position, Quaternion.identity);
  
 }
  
 }
  
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

Answer by LSPressWorks · Mar 31, 2015 at 02:55 PM

From what I can tell of that you're not actually calling the function to apply speed to that object after it's instantiated.

i.e.

 var spawnedBullet : GameObject;
     nextFire = Time.time + fireRate;
     spawnedBullet = Instantiate (BulletPrefab, GameObject.Find ("spawnPoint").transform.position, Quaternion.identity);
     spawnedBullet.RigidBody.AddForce(x,y,x,ForceMode);


Comment
Add comment · Show 2 · 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 dragonlord119 · Apr 07, 2015 at 12:33 PM 0
Share

Sorry. Unfortunately this didn't work. $$anonymous$$onodevelop first asked me to change the colon between var spawnedBullet and GameObject, and when I did the errors flooded in fro mthere. Thanks for trying to help me.

avatar image dragonlord119 · Apr 08, 2015 at 11:54 AM 0
Share

Actually managed to solve the problem. I added a public Vector3 velocity at the top of the script and then added; nextfire = Time.time + firerate

Rigidbody instantiatedProjectile = Instantiate

(BulletPrefab, GameObject.Find

("spawnPoint").transform.position, Quaternion.identity) as

Rigidbody;

instantiatedProjectile.velocity =

transform.TransformDirection (new Vector3 (0, 0, BulletSpeed));

Thanks for answering again though.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

EnemyAI C# Error 1 Answer

bullets kill all enemies, not just the ones hit. 1 Answer

Looping Between Waypoints 1 Answer

How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers

need help making a simple ascending enemy 0 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