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 LPGaming-Company · Feb 13, 2013 at 03:35 AM · javascriptprojectile

Projectile wont move

Here's the script, it's at the bottom.

 var node : Transform;
 var enemies : GameObject[];
 var targetEnemy : GameObject;
 
 var fireAt : Transform;
 var spellTransform : Transform;
 
 var minionSpeed : float;
 var distance : float;
 var attackTimer : float;
 
 var minionHealth : int;
 var minionDamage : int;
 
 var distracted : boolean; // if it's do anything besides walking
 
 function Start() {
     minionSpeed = 1;
     minionHealth = 300;
     minionDamage = 25;
     distracted = false;
     targetEnemy = null;
     attackTimer = 3f;
     node = GameObject.FindGameObjectWithTag("Node").transform;
     enemies = GameObject.FindGameObjectsWithTag("team2");
 }
 
 function Update() {
     if(attackTimer > 0f) attackTimer -= Time.deltaTime;;
     if(!distracted) {
         transform.LookAt(node);
         transform.position += (transform.forward * minionSpeed * Time.deltaTime);
     }
     
     if(enemyInRange()) {
         
         distracted = true;
         attackEnemy();
     }
     //Debug.Log("Closest Enemy: "+FindClosestEnemy().name+ " ---- Distance: " +distance);
 }
 var rotationSpeed2 = 7;
 var moveSpeed2 = 10;
 
 
 function enemyInRange() : boolean {
   enemies = GameObject.FindGameObjectsWithTag("team2tower"); 
   if(enemies.Length > 0) {
         targetEnemy = enemies[0];
         var dist = Vector3.Distance(transform.position, enemies[0].transform.position);
  
         for(var i=0;i<enemies.Length;i++) {
             var tempDist = Vector3.Distance(transform.position, enemies[i].transform.position);
             if(tempDist < dist) {
                 targetEnemy = enemies[i];
                 //Debug.Log("Distance from enemy: "+tempDist);
             }
         }
    }
    
    if(tempDist < 5) {
            return true;
    }
     return false;
 }
 
 function attackEnemy() { 
     if(attackTimer < .1f) {
         sendAttack();
         attackTimer = 3f;
     }
 }
 
 function sendAttack() {
     Debug.Log("Attack sent:");
     fireAt = targetEnemy.transform;
     var attack = Instantiate(spellTransform,this.transform.position,Quaternion.identity);
     attack.rotation = Quaternion.Slerp(attack.rotation,
     Quaternion.LookRotation(fireAt.position - attack.position), rotationSpeed2*Time.deltaTime); 
     attack.position += spellTransform.forward * moveSpeed2 * Time.deltaTime;
     
 }

trying to figure out why it's not moving.

Comment
Add comment · Show 3
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 LPGaming-Company · Feb 13, 2013 at 09:28 PM 0
Share

$$anonymous$$ore than sure. I've been editing it none stop and the changes apply aswell.

Also, that's standart to "$$anonymous$$ultiply" the vector 3 and a float, don't ask me. I took it straight from the wiki.

avatar image vbbartlett · Feb 13, 2013 at 09:46 PM 0
Share

Ignore Ben's comment it is off base and adds nothing to your problem. That part of your code is at least syntactically correct. They don't seem to understand the C# language or Vector math.

avatar image Benproductions1 · Feb 14, 2013 at 05:41 AM 0
Share

Whoops, didn't realise you were adding a Vector3 not a float, sorry about... The reason your projectile won't move, is because nowhere in your script are you telling it to do so. sendAttack only instantiates one projectile and moves it once. You have to make another script to do the moving, or (a terrible idea that works) keep an array of spells, and update very one of them every frame.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vbbartlett · Feb 13, 2013 at 04:35 PM

Not sure where to begin!

The design seems incorrect. You have several transforms one being the spell transform, but it isn't pointing to anything. You don't locate another object to that position and rotation, you aren't instantiating an actual spell prefab... you are creating a transform... nothing else. On top of that once you create it you set its position and rotation once and it never updates. Transforms don't just update themselves.

You probably should start with creating a script that controls a spell, and apply it some some visual object then instantiate that. The script should then update itself each frame with where it wants to move, whether a target or a direction...

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 LPGaming-Company · Feb 13, 2013 at 09:28 PM 0
Share

But, the script does exactly that...

attackEnemy() calls sendAttack() and attackEnemy() is called in Update(), therefor sendAttack() is ALSO ran through update. I am instantiating a transform (I have it set in the inspector, it spawn the item I wanted it to original spawn), I took this directly from another person's code that was released so I could make my own script, so i know that it works as I've seen it before. The way I'm writing everything I can't just have a script that controls a "Spell" because in reality it's just a basic auto attack that sends out a projectile. Although, I do have a script that is attatched to the spell once it is created and that handles all the collision.

avatar image vbbartlett · Feb 13, 2013 at 09:34 PM 0
Share

Ok, that added info helps, but I still don't see where the new instanciated spellTransform gets updated every frame. It is created and set once but that transform doesn't update its position and rotation at any point in the code you have provided.

avatar image vbbartlett · Feb 13, 2013 at 09:41 PM 0
Share

One point of note on your Slerp, if that code were to be called every frame, the rotation of your object would slow down and would never technically reach the lookat direction.

If that is what you want, fine, but normally I would have expected to save the Initial rotation and then increment the slerp float value by the delta time.

Another note since this appears to be moving if there is physics, you should code this movement in the FixedUpdate so that the physics moves correctly and the display doesn't jitter.

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

11 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

Related Questions

UnityEngine.Rect' does not have a visible constructor that matches the.... 1 Answer

How to create random movement in 2D 2 Answers

Coroutine gets skipped in javascript/unityscript 0 Answers

Get all Game Objects near point 2 Answers

Why are my GUI Buttons Triggering automaticaly 3 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