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 /
avatar image
0
Question by nickostan · Jan 24, 2016 at 03:58 PM · 2dprojectile

Instantiate projectile with velocity vector of direction character is facing

Yes, I know, there are a million threads with this exact question. I have followed every single one of them and finally came here to ask what the heck is wrong.

I have a 2D topdown game, with a player that rotates to face the mouse position.

When I click, it instantiates an object which I then want to set a velocity, or AddForce such that it moves in the direction the player was facing at the time of instantiation.

I have tried this several ways:

  1. A script attached to the empty gameObject in front of my character that acts as a spawner, which instantiates the projectile retrieves its reference, and then adds the force/sets velocity on its rigidbody2D component.

  2. A script attached to the empty gameObject/spawner, as above, and a separate script attached to the prefab projectile itself which sets the velocity in the Start() method.

Neither are working. The projectile gets instantiated, but does NOT move at all. The only way I can get it to move is by setting the velocity or addforce to a new Vector2 with constant x,y coordinates i.e. (1, 1).

Here is my sample code:

SAMPLE CODE ATTACHED TO THE "SPAWNER", IN FRONT OF MY CHARACTER;

 using UnityEngine;
 using System.Collections;
 
 public class PlayerToxinController : MonoBehaviour {
 
 public GameObject toxin;
 public Transform spawner;
 public float speed = 10.0f;
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 if (Input.GetMouseButtonDown (0)) {
 
 GameObject clone = Instantiate (toxin, spawner.position, spawner.rotation) as GameObject;
 Rigidbody2D toxin_rb = clone.GetComponent<Rigidbody2D> ();
 toxin_rb.AddForce (spawner.forward * speed);
 
 }
 }
 }

​ Of course these two scripts are not active at the same time, I'm just showing the ways I've been trying and it's not working. Can't get the projectile to move unless I set a static Vector2.

Thanks for any help! Cheers

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by nickostan · Feb 01, 2016 at 05:12 PM

use transform.up for 2D games.

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 Eno-Khaon · Feb 01, 2016 at 07:01 PM 0
Share

"Forward" is based on depth, which a 2D game does not have.

Depending on your sprite layout, you'll want to use up/right/-up/-right ins$$anonymous$$d.

(Note that Vector3 includes up/down/left/right/forward/back, whereas Transform includes only up/right/forward)

avatar image
0

Answer by JoshuaMcKenzie · Feb 01, 2016 at 09:36 PM

you might want to increase the speed quite a bit as well, depending on the mass of your projectile, 10 might not even be enough to make it budge. start with 10000 and half or double it based on if its too fast or too slow.

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 Eno-Khaon · Feb 01, 2016 at 10:00 PM 0
Share

Or, a much more elegant solution:

 toxin_rb.AddForce (spawner.up * speed, Force$$anonymous$$ode.VelocityChange);

Force$$anonymous$$ode includes Impulse and VelocityChange variations to immediately modify velocity without regard to frame rate.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Projectile Ricochet After Collision2d 1 Answer

Raycasting not working as expected: rays are perpendicular to projectile's trajectory and collision doesn't occur 1 Answer

How to calculate projectile range 2 Answers

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer

Aiming a shot in Unity 2d? 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