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 Digital-Phantom · Jan 01, 2015 at 12:31 PM · raycastplayercoroutinefunctionraycasthit

Why is this Coroutine not working ?

Can somebody tell me why my Shoot() coroutine is working? The cannon tracks the player perfectly but doesn't fire. Its as if it doesn't detect the player. Have I done something wrong with raycast? I'm using the built in FPS Controller, is it to do with that maybe?

(The code within the function works fine (I adapted it from another script I'm using)

 var LookAtTarget : Transform;
 var rotate;
 var rotationDampening = 2.0;
 var range = 50.0;
 var distanceToPlayer;
 var hit : RaycastHit;
 var forward;
 public var ionBallPrefab : Rigidbody;
 public var ionCannonEnd : Transform;
 var ionCannon : AudioClip;
 
 function Awake()
 {
     LookAtTarget = GameObject.FindWithTag("Player").GetComponent(Transform);
 }
 
 function Update()
 {
     forward = transform.TransformDirection(Vector3.forward);
 
     distanceToPlayer = Vector3.Distance(LookAtTarget.position, transform.position);
     {
         if(distanceToPlayer <= range) // Check to see if player/Target is within range
         {
             if(LookAtTarget)
             {
                 rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
             }
             if(Physics.Raycast(transform.position, forward, hit, range))
                 if(hit.collider.gameObject.tag == "Player") //Shoot at Player
 
                 Shoot(); //Perform Shoot function
         }
         else
             rotate = Quaternion.identity;
             
             transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * 
 rotationDampening);
     }
 
 }
 
 function Shoot()
 {
         var ionBallInstance : Rigidbody;
         ionBallInstance = Instantiate(ionBallPrefab, ionCannonEnd.position, ionCannonEnd.rotation);
         ionBallInstance.AddForce(ionCannonEnd.forward * 900);
         audio.PlayOneShot(ionCannon);
 }

Suggestions please guys ?

Comment
Add comment · Show 6
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 AlucardJay · Jan 01, 2015 at 01:33 PM 1
Share

you are storing the forward before applying rotation (if LookAtTarget). Try calculating the forward after this, or simply use transform.forward ins$$anonymous$$d of forward in the raycast.

There could be other issues such as the position of the cannons pivot, causing the ray to be cast from the base, or somewhere else where it is not reaching the player, but detecting something else first.

The first thing to always do when debugging a raycast is to see what it hit.

 if ( Physics.raycast( .......... ) )
 {
     Debug.Log( hit.collider.gameObject.name ); // what did the ray hit??
     // Do Stuff
 }


avatar image Digital-Phantom · Jan 01, 2015 at 01:47 PM 0
Share

I'm getting no Debug log at all ? (my bad, placed code incorrectly)

It says I'm hitting cylinder (basically the turret of the cannon) Turning off the cylinders collider and it tracks/locks correctly (get DebugLog for "Decoy")

avatar image AlucardJay · Jan 01, 2015 at 02:15 PM 1
Share

In that case, move the origin of the raycast so it doesn't intersect with the cannon, something like :

 if(Physics.Raycast(transform.position + (transform.forward * 1.5), transform.forward, hit, range)) // change 1.5 to suit

Ideally, the cannon and player colliders would be on different layers, and the raycast would be done with a layermask parameter (to ignore hits on specified layers)

http://unity3d.com/learn/tutorials/modules/intermediate/layers

http://docs.unity3d.com/$$anonymous$$anual/Layers.html

http://docs.unity3d.com/ScriptReference/Layer$$anonymous$$ask.html

http://docs.unity3d.com/$$anonymous$$anual/class-Tag$$anonymous$$anager.html

avatar image Digital-Phantom · Jan 01, 2015 at 02:16 PM 0
Share

Worked fine when I just asked it to give me a Debug.Log (well at least showed me what I was hitting)

As soon as I asked it to do something I get this error-

NullReferenceException: Object reference not set to an instance of an object Turret $$anonymous$$ovement.Update () (at Assets/Scripts/Turret $$anonymous$$ovement.js:32)

avatar image AlucardJay · Jan 01, 2015 at 02:25 PM 1
Share

$$anonymous$$ake sure you have assigned ionBallPrefab, ionCannonEnd, ionCannon, in the Inspector.

http://answers.unity3d.com/questions/47830/what-is-a-null-reference-exception-in-unity.html

http://answers.unity3d.com/questions/528288/null-reference-exception-what-is-it-and-why-do-i-g.html

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by HarshadK · Jan 01, 2015 at 12:38 PM

Physics.Raycast does not detect a raycast hit against the Character Controller so you need to have an other type of collider like a capsule collider on your player. This may create some unexpected behavior sometimes with the movement and collisions.

You can also try: RigidbodyFPSWalker instead of Character Controller.

Comment
Add comment · Show 3 · 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 Digital-Phantom · Jan 01, 2015 at 01:20 PM 0
Share

Think I'm being dumb here but could you/somebody please explain that assu$$anonymous$$g I'm only 5 years old...lol

I've tried adding capsule, mesh and box colliders to the FPS controller (and the graphics component) and still nothing. Yes I got some weird movement so I tried just placing the player within range. The turret still tracked ok but didn't fire?

Do I have to remove anything from the FPS controller or simply add the new collider? (if so... where)

???

avatar image Digital-Phantom · Jan 01, 2015 at 01:32 PM 0
Share

O$$anonymous$$ I tried a different approach. I made a completely new object (sphere) so it obviously has all the colliders. Reset the script to look for and track this "Decoy" I tagged it accordingly. The cannon tracks the Decoy but does not fire once locked on.

???

avatar image AlucardJay · Jan 01, 2015 at 01:34 PM 1
Share

Raycast does not work with the Unity FPS character controller ??

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

26 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

Related Questions

Raycast Not Drawing In Target Direction? 0 Answers

Raycasts don't go the right direction unless i'm really far away (javascript) 1 Answer

Crash in : UnityEngine.RaycastHit:INTERNAL_CALL_CalculateRaycastTexCoord 1 Answer

Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer

Opening Door With Raycast 2 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