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 Endiss · Mar 04, 2013 at 04:04 PM · c#rotationshootingvector

Shooting problem the bullets fly in diffrent direction then player looks?

Hello the problem is as topic says the bullets shooted flys in difrent direction as they should so maybe some profesional unity developer could help me solve this out trying to this whole day and im out of ideas...

Shooting & movment script: using UnityEngine; using System.Collections;

 public class characterMovment : MonoBehaviour {
     #region --Pubs--
     public bool Run {
         get
         {
             return _run;
         }
         set
         {
             _run = value;
         }
     }
     public GameObject ammo;
     public string weaponName = "H&K";
     public int weaponMinDmg = 4;
     public int weaponMaxDmg = 15;
     public float moveSpeed = 6.0F;
     public float runSpeed = 9.0F;
     public float gravity = 20.0F;
     #endregion
     #region --Privs--
     private bool _run;
     private Vector3 _moveDirection = Vector3.zero;
     #endregion
     
     // Use this for initialization
     void Start () {
         Run = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown(KeyCode.T))
         {
             if(_run)
             {
                 Run = false;
             }
             else
             {
                 Run = true;
             }
         }
         CharacterController controller = GetComponent<CharacterController>();
         if(controller.isGrounded)
             {
                 _moveDirection = new Vector3(0,0,Input.GetAxis("Vertical"));
                 _moveDirection = transform.TransformDirection(_moveDirection);
                 if(!_run)
                 {
                     _moveDirection *= moveSpeed;
                 }
                 else
                 {
                     _moveDirection *= runSpeed;
                 }
             }
             _moveDirection.y -= gravity*Time.deltaTime;
             controller.Move(_moveDirection*Time.deltaTime);
             transform.RotateAround(new Vector3(0,Input.GetAxis("Horizontal"),0)*Time.deltaTime,Time.deltaTime);
         if(Input.GetKeyDown(KeyCode.F))
         {
             Instantiate(ammo,transform.position,transform.rotation);
         }
     }
 }


Bullet fly script:

 using UnityEngine;
 using System.Collections;
 
 public class bulletMove : MonoBehaviour {
     
     #region --Pubs--
         public int bulletRange = 10;
         public float bulletSpeed = 4;
     #endregion
     #region --Privs--
         private Vector3 startPos;
         private GameObject shooter;
     #endregion
     
     // Use this for initialization
     void Start () {
         shooter = GameObject.FindGameObjectWithTag("Player");
         startPos = shooter.transform.position;
 
     }
     // Update is called once per frame
     void Update () {
         float distance = Vector3.Distance(startPos,transform.position);
         Debug.Log(distance);
         float move = bulletSpeed * Time.deltaTime;
         transform.Translate(transform.forward * move);
         if(distance >=bulletRange)
         {
             Destroy(gameObject);
         }
     }
 }
 
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

Answer by selzero · Mar 04, 2013 at 06:10 PM

Maybe "forward" is not the one you are looking for, try "right" and "up" and also multiplying the vector by -1

(I cant see your game so not sure which direction you need)

Use Debug.Log() and see whats happening.

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

Answer by robertbu · Mar 04, 2013 at 11:23 PM

Forward is the side of your object facing positive Z. Nothing in your code rotates the forward vector of the bullet to match the target. As the last line in the Start() method of your bulletMove script, add:

 transform.LookAt(startPos);

If for some reason you do not want to rotate your bullet to face your target, you could do change your translate code to:

  transform.Translate((startPos - transform.position).normalized * move);
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

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

Multiple Cars not working 1 Answer

Flip over an object (smooth transition) 3 Answers

[C#]Angle of shooting equale to mouse direction 1 Answer

Wrong rotation while moving 1 Answer

Problem with shooting an object in 2D 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