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 Vincent-Terra · Nov 08, 2012 at 05:44 PM · c#instantiateworld spaceprojectileslocal space

Problem with bullet projectiles direction

Basically the bullets seem to instantiate in a certain direction which doesn't change but I don't seem to be able to find what's wrong. I believe it has to do with world space and local space. This is my code so far

 using UnityEngine;
 using System.Collections;
 
 public class Artificial_Intelligence : MonoBehaviour {
     
 private GameObject player;
 public int rotation_speed;
 public int speed;
 public int power;
 public int spawndistance;
 public GameObject prefab;
     
     // Use this for initialization
     void Start () {
      player = GameObject.Find("Player");
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Vector3.Distance(player.transform.position, transform.position) < 7) {
         Quaternion finalrotation = Quaternion.LookRotation(player.transform.position - transform.position, Vector3.up);
     finalrotation.x = 0;
     finalrotation.z = 0;
     transform.rotation = Quaternion.Slerp(transform.rotation, finalrotation, Time.deltaTime * rotation_speed);
     Instantiate (prefab,transform.position, transform.rotation);
     prefab.rigidbody.AddForce(Vector3.forward * power);
     Physics.IgnoreCollision(prefab.collider, collider);
     transform.Translate(Vector3.forward * Time.deltaTime * speed);
         }
     }
Comment
Add comment · Show 2
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 KiraSensei · Nov 08, 2012 at 07:11 PM 0
Share

I think the problem is that you instantiate your prefab (i suppose the projectile) with the position and rotation of the parent of this script, ins$$anonymous$$d of player's. Can you tell me what this script is attached to ?

avatar image valyard · Nov 08, 2012 at 07:28 PM 0
Share

Shouldn't it be prefab.rigidbody.AddForce(transform.rotation Vector3.forward power) ?

2 Replies

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

Answer by Seth-Bergman · Nov 08, 2012 at 07:33 PM

there are a couple of issues here:

 //you need to declare a reference to the instantiated object
 GameObject myBullet = Instantiate (prefab,transform.position, transform.rotation) as GameObject;
 // use transform.forward for the relevant direction
 myBullet.rigidbody.AddForce(myBullet.transform.forward * power);
 Physics.IgnoreCollision(myBullet.collider, collider);
 transform.Translate(Vector3.forward * Time.deltaTime * speed);

mainly, for AddForce to work right we need to convert our local direction to a world direction, which is what transform.forward does

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 Seth-Bergman · Nov 08, 2012 at 07:39 PM 0
Share

also, you are instantiating a bullet every frame, that will add up quickly.. you should probably cut that back..

avatar image
0

Answer by JustinRyder · Nov 08, 2012 at 08:22 PM

 rigidbody.AddForce(Vector3.forward, Space.Self);
 
 rigidbody.AddForce(transform.forward);

These will both add a forward force in local space.

Comment
Add comment · 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

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

13 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 avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Create variable based on local axis 1 Answer

Distribute terrain in zones 3 Answers

instantiate object 2 Answers

Navmesh error on instantiated navmesh agents 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