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 Hydropulse17 · Sep 23, 2018 at 08:06 PM · raycast2d-physicsbullets

Raycast attracted to scene origin (0,0,0)

I'm experimenting with a top-down 2D shooter gameplay. The player character aims at the cursor and fires high velocity bullets. From one frame to the other the bullets were skipping over my enemies so I decided to use raycast instead of colliders.

I thought what I had was working fine but I realised that enemies were dying no where near my bullets and upon further inspection I realised the rays were stretching to the world origin, which makes the bullet effect a wide margin. It was difficult to get a quality screenshot but you can see what I mean here.

alt text

I know this has something to do with the difference between points and directions, but I don't rightly understand and I've tried all kinds of stuff to figure it out. Even trying linecast instead was producing strange results.

Here is all the relevant code.

 public class Bullet : MonoBehaviour {
 
     public Vector3 speed = new Vector3(0f,1f,0f);
     
     void FixedUpdate ()
     {
         Vector3 nextPos = transform.position + speed;
         RaycastHit2D hit = Physics2D.Raycast(transform.position, (nextPos.normalized));        
         Debug.DrawRay(transform.position, nextPos.normalized);
     }    
 }

Currently I have it normalised because I read that was the difference between a point and a direction but again I have no understanding of what's happening, and clearly it didn't work. If it's not clear, what I want to happen is for the raycast to start at the bullet and trail behind it a bit.

untitled-1.jpg (86.3 kB)
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 LCStark · Sep 23, 2018 at 08:13 PM

You want to generate a ray from your current position in the direction you are moving. To get that direction, you have to subtract your current position from your next one.

Vector3 nextPos = transform.position + speed;
Vector3 direction = nextPos - transform.position;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction.normalized);

You are currently using your objects nextPosition as the direction. When you do that, that position is treated as a vector starting in the coordinate system origin (0,0,0) going towards that position. When you normalize it, it still starts in the same place and goes towards the same direction.

Comment
Add comment · Show 2 · 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 Hydropulse17 · Sep 23, 2018 at 08:43 PM 0
Share

After applying that change, the ray extends straight up. It might be relevant that the bullets are instantiated with the same rotation as the player. This had me thinking about world space / local space. I think my next position calculation is the problem because as far as the raycast is concerned the next position is 1 unit above the bullet. That being said, the actual bullet prefab does move as I expect it to. I tried playing with transform.localPosition but it doesn't seem to work that way.

avatar image LCStark Hydropulse17 · Sep 23, 2018 at 08:56 PM 0
Share

Yes, that would be the problem. You should also rotate your speed. This should do the job:

Vector3 nextPos = transform.position + transform.rotation * speed;

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

133 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

Related Questions

Bullet collision help 1 Answer

Use 2D Effectors with Raycast Collision Method 0 Answers

2D raycast doesn't work with trigger colliders? 1 Answer

Most efficient way to make tracer machingun fire? (raycast or rigidbody) 1 Answer

Unity Raycast2D results are not as expected? 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