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 mwozniak93 · Nov 24, 2016 at 04:27 AM · rigidbody3dshootingdirection

Enemy shoots player (Tower)

Hello!

I have a problem with Enemy (Tower) shooting player

I use this code (which I read that half of the world use to shoot the target :) ):

 Vector3 shootPos =(CurrentTarget.transform.position - this.transform.position).normalized;
 bullet.rigidbody = shootPos * acceleration;

But unfortunetly what I get as a result is Tower shooting bullet towards player but in wrong "Y" direction. It always goes too high - I mean the bullet is fired from the top of the Tower (let's say y=10) and my player (the target for tower) position y=3. The bullet should shoot him but instead it always go above the player.

Please help me with this issue - I wasted 5 hours for this. Thank you in advance

Comment
Add comment · Show 3
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 Link0n3 · Nov 24, 2016 at 05:03 PM 0
Share

@kunmateo1993 There are not enough information to detect the problem (i.e. when and where the code you presented is called?). If you are using rigid body to move the bullet game object expect the physics to be applied to this, so things like gravity could be relevant in the process (although I would expect and undershoot).

 bullet.rigidbody = shootPos * acceleration;

Don t think this is correct. $$anonymous$$aybe are you updating the velocity? Also

 Vector3 shootPos =(CurrentTarget.transform.position - this.transform.position).normalized;

this.transform.position).normalized; should be named shootDirection, but I guess you understand what you are calculating there.

There are two things that you could try in order to solve your problem:

  • Assu$$anonymous$$g you are doing this on a some Update(): bullet.transform.Translate (shootPos * speed * Time.deltaTime);

  • Assu$$anonymous$$g you are doing this in a method that is called once I would try to set a force to the rigid body: bullet.rigidbody.AddForce(shootPos * forcestrength);

Hope it is clear enough.

Andrea

avatar image mwozniak93 Link0n3 · Nov 24, 2016 at 06:43 PM 0
Share

I am sorry - previously I wasn't clear (forgive me it was late in the night :) )

Shoot function is called once, not in update. Gravity is Off.

I have one shoot function which is called by player and by enemy but with two different parameter's value.

For player I use this as parameter:

 Ray ray = $$anonymous$$ainCamera.ScreenPointToRay (_tmCursor.position);
         shootDirection = new Vector3 (ray.direction.x, ray.direction.y, ray.direction.z);


And it works just fine - bullet goes where the Cursor was pointing.

In case of Enemy I have this:

 Vector3 shootDirection =(CurrentTarget.transform.position - transform.position).normalized;


And it causes that

 bullet.ctRigidbody.velocity = (shootDirection * oCharacter.CurrentSkill.Acceleration);


works like I described previously - bullet goes above player so he cant be hit.

Please, if you come up with some idea after this additional information let me know. AddForce doesnt work on "Click" because my bullet wasnt moving. AS I remember correctly addFroce works in Update().

avatar image Link0n3 mwozniak93 · Dec 01, 2016 at 02:53 PM 0
Share

@kunmateo1993 So I am assu$$anonymous$$g you are doing an FPS. The shootdirection seems correct to me. Have you check a very trivial thing:

  • where the transform of CurrentTarget object is in relation to your main camera position?

  • Have you changed some parameters to the camera?

0 Replies

· Add your reply
  • Sort: 

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

Not shooting in desired direction 1 Answer

How to make a player rotate in a direction with a slope of the ground. 0 Answers

Making laser beam point in shooting direction C# 1 Answer

For some akward reason bullet is moving in the wrong direction 1 Answer

How can the player position updates randomly when the player isn't movingl? 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