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 AndersNOR · Jan 18, 2016 at 04:28 PM · instantiatespeeddirectionbullettargeting

How do I instantiate an objects direction and speed seperately?

I have created a bullet spawner class that can shoot bullets in a set direction, but now I am trying to add a mode where it gets the players position and fires a bullet towards the player.

I have managed to make it fire towards the player, but the bullets speed is way to high when the player is far away and way to slow when it is close to the spawner. How do i instantiante the bullets speed seperately from its direction?

Or is there a better way to get the correct vector towards the player?

below is my class as a whole.

 using UnityEngine;
 using System.Collections;
 
 
 public class bulletSpawner : MonoBehaviour
 {
 
     
     public GameObject Bulletprefab; //Which bullet to use
     public GameObject spawner; //the spawner that creates the bullet
     public Rigidbody2D rigidbody2D; //Create the rigidbody variable
     public float speed; // The starting speed of the bullet
     public Rigidbody2D clone; //Gives acces to the rigigbody of the clone created
     public Vector2 direction; //the direction of the bullet
     public float gravityScale; //how much gravity shall affect the bullet. 0 if no gravity
     public bool findTarget; //if the bullet should fire towards the player on instantiate
     public Transform Player; //the players position
     public float maxRange; //if player is closer than the maxRange, it can fire 
 
 
 
 
     public float timer; //how many seconds until the spawner starts shooting
     public float restart; //How many seconds between each shot
 
 
 
     // Use this for initialization
     void Start () 
     {
         
         rigidbody2D = Bulletprefab.GetComponent<Rigidbody2D>(); //get the rigidbody2D component attached to the object
 
 
     
     }
 
 
     
     // Update is called once per frame
     void Update () 
     {
         timer = timer - 1 * Time.deltaTime; //withdraws 1 from the timer each second
 
             if (timer <= 0.0f)
         {
             
                 //Rigidbody2D clone;
             if (findTarget)//if it shall fire towards the player
             {
 
 
                 if (Vector2.Distance (transform.position, Player.position) <= maxRange)//check if the player is closer than maxRange
                 {
                     //get the angle between player and spawner
                     direction = new Vector2 (Player.position.x - spawner.transform.position.x, Player.position.y - spawner.transform.position.y); 
                     //Create a clone of the bullet as a Rigidbody2D
                     clone = Instantiate (rigidbody2D, spawner.transform.position, spawner.transform.rotation) as Rigidbody2D; 
                     //Set the speed and direction of the object.x
                     clone.velocity = transform.TransformDirection(direction*speed);
                     //Sets the value on how much the gravity shall affect the object. 0 if there is no gravity
                     clone.gravityScale = gravityScale;
                     timer = restart; //restart the timer
                 } else
                     timer = restart; //restart the timer
                     return;
             }
                 
             else//if findTarget is false, fire towards a predefined direction
             //Create a clone of the bullet as a Rigidbody2D
             clone = Instantiate (rigidbody2D, spawner.transform.position, spawner.transform.rotation) as Rigidbody2D; 
             //Set the speed and direction of the object
             clone.velocity = transform.TransformDirection (direction * speed);
             //Sets the value on how much the gravity shall affect the object. 0 if there is no gravity
             clone.gravityScale = gravityScale; 
 
             timer = restart; //restart the timer 
         }
     
     }
 
         
 }
 

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
Best Answer

Answer by NoseKills · Jan 18, 2016 at 05:29 PM

Naturally, if you set the distance vector from shooter's position to target's position as the velocity, it will be the greater the farther the target is.

You need to normalize the direction vector ( make its length 1) before multiplying with 'speed' if you want the velocity to be of the same magnitude every time.

 clone.velocity = transform.TransformDirection(direction.normalized*speed);
Comment
Add comment · Show 1 · 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 AndersNOR · Jan 18, 2016 at 05:40 PM 0
Share

Wow, it was that easy... I've been trying several different solution today without any success, and all that was needed was to add "normalized" to it all. Thanks a lot!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers

How does one continuously move an object forward without using velocity? 1 Answer

Bullet has wrong direction on client 0 Answers

Instantiate vs Instantiate as gameobject 1 Answer

Aiming and shooting to the same point in third person 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