• 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
Question by cassius · Feb 12, 2012 at 06:18 PM · instantiatevelocityaddforceprojectile

Velocity and AddForce Problems

I'm hoping someone can reveal what I'm doing wrong in my script that I'm using to instantiate a weapon's projectile and set a velocity (or force). What's really strange is that I have almost this exact code working on another "enemy" and it's working fine.

I've tried with both "weaponClone.velocity" and "weaponClone.rigidbody.AddForce" but neither apply any motion to the projectile. Projectiles are instantiated no problem and otherwise act as I expect.

/* This script controls the enemy's weapon firing. It instantiates the projectile and puts it into motion. Usually towards the Player's location. */

// Require the weapon to be a rigidbody, That way we know a prefab cannot be assigned without rigidbody var weapon : Rigidbody; // We'll likely want to fire at the Player, so lets create a variable so we can locate the player var player : GameObject; // Specify how many seconds between each enemy weapon var fireRate = 1.00; // Tells us how many weapons have been fired by the enemy (not total, just since last fireRate) var fired = 0; // This variable will hold the time.time + time between firing. private var nextFire = 0.0; // Count the total number of weapons fired by this Enemy var totalWeaponsFired : float = 0; // Set the speed that the weapon travels at var weaponSpeed = 100; // Set the weapons to be disabled in the beginning private var weaponsActive = false;

// Cache the transform private var myTransform : Transform; function Awake () { myTransform = transform; }

function Start () { // Set the 'player' variable to find the Player's ship player = GameObject.Find("Ship"); }

function FireWeapon () { // Instantiate the weapon and start it's position at the same location as the object holding this script var weaponClone : Rigidbody = Instantiate(weapon, myTransform.position, myTransform.rotation); // Turn off gravity to this object's rigidbody weaponClone.rigidbody.useGravity = false; // If there's no collider and we need to add one, we use this... weaponClone.transform.gameObject.AddComponent(BoxCollider); // or MeshCollider // If we use the box collider, we need to specify a size for it. weaponClone.collider.size = Vector3(0.005,0.005,0.005); // Set the collider so that it's a trigger. Don't want the weapon bouncing off stuff weaponClone.collider.isTrigger = true; // Set a name for the instantiated Enemy weapon weaponClone.name = "VTOL Weapon"; // Set the speed and direction of the weapon //weaponClone.rigidbody.AddForce(myTransform.forward weaponSpeed); //weaponClone.velocity = (player.transform.position - transform.position).normalized weaponSpeed; weaponClone.velocity = (player.transform.position - transform.position)* weaponSpeed;

 // Keep count of the total number of weapons fired by the Enemy
 totalWeaponsFired++;

}

function FixedUpdate () { // Lets see if the Player even exists. No point firing if they're dead. if(player) { // Now lets see if the weapons are active if(weaponsActive == true) { // See if we have already fired if(fired <= 0) { // Enemy hasn't fired yet so they should fire a weapon FireWeapon(); // update the time (in seconds) when the Enemy can fire again nextFire = Time.time + fireRate; fired++; } // Enemy has fired if(nextFire <= Time.time) { // Reset the number of currently fired weapons fired = 0; } } } }

function WeaponsEnable() { weaponsActive = true; }

function WeaponsDisable() { weaponsActive = false; }

Comment

People who like this

0 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 cassius · Feb 12, 2012 at 07:05 PM 0
Share

Oh, I should point out that function WeaponsEnable() is called by another script, so the weaponsActive variable is indeed true. Weapons are fired, they just don't move. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

instantiate finding upstream velocity 1 Answer

Angle and Velocity in a Projectile 1 Answer

one gameobject instantiaton not 100 3 Answers

Rigidbody.AddForce not working 1 Answer

[C#] Recreating object A's velocity relative to object B 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