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 enricogp · Jun 29, 2020 at 09:06 PM · collisionprojectilefirst-person-controllerfirst-personmiddle

How should I make the projectiles in a fps go straight to the middle of a screen? also, collision problems

I"m trying to make a FPS game using Unity Standard Assets, in which the user shoots arrows with a bow, requiring the arrows to be projectiles (contrary to a raycast-only fps).

I figured out how to make the arrows fire from a point of the bow and go straight somewhere, but what I actually want is making so that the arrows spawn from the bow and go to where the player is aiming (aka the middle of the screen).

I followed some similar questions here on Unity Answers, but for some reason the arrows don't go exactly to where the point is (they actually differ a lot from where the player is aiming). Additionally, sometimes projectile collisions won't register (the arrow sometimes passes through solid objects).

here is the code added code contained in the fps controller script that makes the player shoot arrows:

Ray ray = m_Camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0)); RaycastHit hit; // Check whether your are pointing to something so as to adjust the direction Vector3 targetPoint; if (Physics.Raycast(ray, out hit)) targetPoint = hit.point; else targetPoint = ray.GetPoint(1000); // You may need to change this value according to your needs // Create the bullet and give it a velocity according to the target point computed before

                             GameObject arrowProjectile = Instantiate(m_ArrowPrefab, m_ArrowSpawn.position, m_ArrowPrefab.transform.rotation);
                             Quaternion rotationOffset = Quaternion.Euler(270f, 0f, 0f);
                             arrowProjectile.transform.rotation = m_Camera.transform.rotation * rotationOffset;
                             Rigidbody arrowRB = arrowProjectile.GetComponent<Rigidbody>();
                             m_ArrowList.Add(arrowProjectile);


                             arrowRB.velocity = (targetPoint - m_ArrowSpawn.transform.position).normalized * m_ShootForce;
                             //arrowRB.velocity = m_Camera.transform.forward * m_ShootForce;

                             UpdateArrowCount() // Checks if arrowList is full. If yes, destroy the oldest arrow object

and here is the code contained in a script attached to the arrow prefab:

 public class Arrow : MonoBehaviour
 {
     [SerializeField] private Rigidbody arrowRB;
     [SerializeField] private Collider arrowCollider;
     private bool hitSomething = false;
     Quaternion m_RotationOffset;
 
     void Start()
     {
         //arrowRB = GetComponent<Rigidbody>();
         m_RotationOffset = Quaternion.Euler(270f, 0f, 0f);
         transform.rotation = Quaternion.LookRotation(arrowRB.velocity) * m_RotationOffset;
     }
 
 
     void Update()
     {
         if (!hitSomething)
         {
             transform.rotation = Quaternion.LookRotation(arrowRB.velocity) * m_RotationOffset;
         }
         
     }
 
     private void OnCollisionEnter(Collision collision)
     {
 
         if (collision.gameObject.tag == "Player" || collision.gameObject.tag == "MainCamera")
         {
             Physics.IgnoreCollision(collision.collider, arrowCollider);
         }
         if (collision.collider.tag != "Arrow" && collision.collider.tag != "Player" && collision.collider.tag != "MainCamera")
         {
             hitSomething = true;
             arrowRB.constraints = RigidbodyConstraints.FreezeAll;
         }
         
     }
 }
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
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jun 29, 2020 at 10:26 PM

Your aiming code looks fine to me; I'm suspicious that your arrow is colliding with something that is altering the trajectory of it. I notice in your "OnCollisionEnter" script you tell the arrow collider to ignore the player and main camera colliders, but you only do it after it has already collided with them and has already altered the physics trajectory. Physics.IgnoreCollision will ignore any FUTURE collisions between the specified colliders, but will not retroactively ignore collisions that have already happened. I recommend you either use Physics.IgnoreCollision() when spawning the arrow, use physics layers and unity's collision matrix in the physics options to ignore collisions between the arrow and the player (this is the easiest to do), or move your arrow spawn point forward so that it doesn't collide with the player on spawn.


As far as your arrow passing through objects - you can still use Raycasting for projectiles. A common practice is to keep track of the arrow's positions each frame as well as its previous position from the frame before then raycast between the two positions to check for a collision. This way your arrow still flies with the same velocity as a projectile, but you will see any colliders that your arrow would normally have missed in between frames. You can also try updating the collision detection options on the rigidbody itself, but I've personally had mixed results doing this.

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 enricogp · Jun 30, 2020 at 12:20 AM 0
Share

Thank you for your answer! You were right, the arrow was indeed coliding with the player, and thus changing trajectories. I used unity's collision matrix and the problem is gone. About arrows passing through objects:I will try your suggestion to see if it works. I did realize, however, that removing those collisions actually makes it happen more rarely now.

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

201 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 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

Dragrigidbody help 1 Answer

Projectiles, their speed, and collisions 1 Answer

Bounce when hitting wall 2 Answers

Gun Fire, sparks on Collision 1 Answer

Bullet projectiles with collision info without affecting others 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