• 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 jokehboy · Mar 04, 2018 at 09:24 PM · rotationshootingforcenooblocal

Shoot projectile straight from player no matter the rotation.

I'm trying to shoot a projectile from the front end of my ship (player) but it always shoots off to the side as soon as I rotate the player. I think it has something to do with the parent/child objects in the player hierarchy but I'm not skilled enough to fix it.


I'll put the prefab hierarchy here:

  • Player(An empty storing box colliers and movement script)

  • Camera

  • Ship model (No scripts or rigidbody attatched)

  • Particle trails x 2

  • shootSpawner (The EMPTY OBJ with the script attached)


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Shoot : MonoBehaviour {

 //The thing being shot and the force it is shot at
 public GameObject projectile;
 public float shootForce;
 public Transform shootPos;
 

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {

     

 }

 public void ShootMethod()
 {

     //Instatntiated from empty and is shot in forward dir.
     //PROBLEM WITH OBJ NOT BEING SHOT STRAIGHT AT ALL TIMES, NEEDS FIXING.

     //AddFORCE ISNT WORKING REMOVED.
         GameObject bullet = Instantiate(projectile, shootPos.transform.position, shootPos.transform.rotation);
         bullet.GetComponent<Rigidbody>().AddForce(transform.forward * shootForce);


     //Add an IEnumerator for fire rate (public float for wait time)

     new WaitForSeconds(5f);
         Destroy(bullet, 5f);
     
 }
 


}

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

Answer by jokehboy · Mar 04, 2018 at 09:39 PM

It seems it's a case of inertia... the object shoots straight but due to the speed of the ship it passes the point of the instantiation making it look off. I changed my code and it will now shoot 'straight' but still look off.


GameObject bullet = Instantiate(projectile, transform.position, transform.rotation); bullet.GetComponent().AddForce(shootPos.forward * -5000);


I know there's a way to shoot from a ray cast and if a reference could be posted that would be great.

Because once I've got the shooting solved I'm turning the ball into a laser beam.

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 dCalle · Mar 04, 2018 at 10:00 PM 0
Share

hmm, did you think about adding your current velocity and deltaRotation to the transform.position / rotation?

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

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

Related Questions

Unity 2D Apply force in Z Direction 0 Answers

Need help with Transform.LookAt 1 Answer

Copying the local rotation attributes to a quaternion? 2 Answers

Quaternion.Slerp on local axis 1 Answer

Move object on local axis instead of world axis after rotation 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