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 TehWut · Dec 24, 2011 at 07:31 PM · raycastraycasthitshoot

Raycasthit.point - same place every time! (Unity 3.5)

Hello. I'm trying implement a raycast shooting system for my FPS game. the problem is, everytime I try to instantiate a spark effect at the point of impact, it returns a location of (0,0,0) and creates the spark at that location no matter where I shoot it. Here is the relevent shooting code. Also, I am using Unity 3.5 (this may be the issue). Please note that the rocket contains no problems, only the Gun type. The object this is attached to is a child of the camera. Sickly code:

  case weaponType.Gun :
    var fwd = transform.TransformDirection (Vector3.forward);
    var hit: RaycastHit;
 if (Physics.Raycast(launchpoint.position,fwd,range))
 Instantiate(explosionPrefab,hit.point,transform.rotation);
 print("this could not be more actual");
 print(hit.point);
 }
 }


/////////////////////////////////// full code:

 private var curReload = 0;
 enum weaponType {Rocket, Gun, Melee}
 var reloadTime = 0;
 var projectile: GameObject;
 var explosionPrefab : Transform;
 var currentWeapon: weaponType;
 var Speed = 30;
 var range = 10;
 var launchpoint : Transform;
 private var countReload = false;
 function Start()
 {
 curReload = reloadTime;
 }
 function Update () {
 print(curReload);
 print(countReload);
 if ((Input.GetButtonDown("Fire1")) && (curReload == reloadTime))
 Fire(currentWeapon);
 if (countReload)
 {
 curReload++;
 }
 else
 {
 return;
 }
 if (curReload == reloadTime)
 {
 countReload = false;
 }
 }
 
 
 function Fire(currentWeapon : weaponType) {
 animation.CrossFade("Shoot",0.2);
 countReload = true;
 curReload = 0;
 switch (currentWeapon)
 {
 case weaponType.Rocket :
 var projed =Instantiate(projectile,launchpoint.position,transform.rotation);
 projed.GetComponent(Projectile).speed = Speed;
 break;
 case weaponType.Gun :
    var fwd = transform.TransformDirection (Vector3.forward);
    var hit: RaycastHit;
 if (Physics.Raycast(launchpoint.position,-fwd,range))
 Instantiate(explosionPrefab,hit.point,transform.rotation);
 print("this could not be more actual");
 print(hit.point);
 }
 }
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
2
Best Answer

Answer by save · Dec 29, 2011 at 07:34 AM

You never declare hit inside your Raycast, that should be the issue.

Sorry for the off topic, Unity 3.5 is released? (oh happy day!) :)

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 TehWut · Dec 29, 2011 at 04:55 PM 1
Share

I think you're pretty much the best ever ;) It works! //////// for others having this problem, change it to something like var hit: RaycastHit; if (Physics.Raycast(transform,rotation,hit)));

avatar image shinriyo_twitter · Aug 27, 2013 at 06:33 AM 0
Share

I guess point is not the position raycast hits. just the gameobject poisition.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a Vector3 from a RaycastHit variable? 1 Answer

Using raycast and collider to increase int 1 Answer

Raycast not working 2 Answers

Raycasting Through Terrain 1 Answer

Raycast exit point of collider 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