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 Harald921 · May 01, 2015 at 04:53 PM · scripting problemraycast

Raycast not detecting anything if MainCamera's X rotation is > 41.5

I am using a raycast script to detect rocks/trees in front of my character so I can chop them, etc, but if I look down (so that X rotation of the main camera is more than 41.5) the raycast will detect absolutely nothing. What is causing this error? I am use the exact same script as I used in my Unity 4 version of the game, and I didn't have this problem back then!

This is the script I am using, and yes I know, sound management in the same script. I am kind of a beginner, and I will improve my scripts when I've got basic gameplay features in the game!

 #pragma strict
 
 var rayLength : int = 10;
 var soundWoodChop1 : AudioClip;
 var soundWoodChop2 : AudioClip;
 var soundWoodChop3 : AudioClip;
 var soundWoodChop4 : AudioClip;
 var soundRockPick1 : AudioClip;
 var soundRockPick2 : AudioClip;
 var soundRockPick3 : AudioClip;
 var soundRockPick4 : AudioClip;
 
 var hasAxe : boolean = false;
 var hasPick : boolean = false;
 var canSwing : boolean = false;
 
 private var randomNumber : int = 0;
 private var animController : animationController;
 
 var tree : GameObject;
 var rock : GameObject;
 
 function Start()
 {
     animController = GameObject.Find("gameManager").GetComponent(animationController);
     hasAxe = true;
     hasPick = false;
     canSwing = true;
 }
 
 function Update()
 {
         var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 10;
         Debug.DrawRay (transform.position, forward, Color.green);
     
     if (animController.canSwing)
     {
         canSwing = true;
     }
     
     else
     {
         canSwing = false;
     }
     
     if (Input.GetKeyDown(KeyCode.F))
     {
         hasAxe = true;
         hasPick = false;
     }
     
     else if (Input.GetKeyDown(KeyCode.G))
     {
         hasPick = true;
         hasAxe = false;
     }
     
     var hit : RaycastHit;
     var fwd = transform.TransformDirection(Vector3.forward);
     
     if (Physics.Raycast(transform.position, fwd, hit, rayLength))
     {    
         if (hit.collider.gameObject.tag == "Tree" && hasAxe)
         {
             tree = (hit.collider.gameObject);
             animController = GameObject.Find("gameManager").GetComponent(animationController);
             
             if (Input.GetButtonDown("Fire1") && canSwing)
             {
                 canSwing = false;
                 playWoodChopSound();
                 tree.GetComponent(TreeController).treeHealth -= 1;
             }
         }
         
         else if (hit.collider.gameObject.tag == "Rock" && hasPick)
         {
             Debug.Log("you are looking at a rock");
             rock = (hit.collider.gameObject);
             animController = GameObject.Find("gameManager").GetComponent(animationController);
             
             if (Input.GetButtonDown("Fire1") && canSwing)
             {
                 canSwing = false;
                 playRockPickSound();
                 rock.GetComponent(rockController).rockHealth -=1;
             }
         }
     }    
 }
 
 function playRockPickSound()
 {
     randomNumber = Random.Range(1,5);
     
     if (randomNumber == 1)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundRockPick1);
     }
     
     if (randomNumber == 2)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundRockPick2);
     }
 
     if (randomNumber == 3)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundRockPick3);
     }
     
     if (randomNumber == 4)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundRockPick4);
     }
 }
 
 function playWoodChopSound()
 {
     randomNumber = Random.Range(1, 4);
     
     if (randomNumber == 1)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundWoodChop1);
     }
     
     else if (randomNumber == 2)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundWoodChop2);
     }
     
     else if (randomNumber == 3)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundWoodChop3);
     }
     
     else if (randomNumber == 4)
     {
         GetComponent.<AudioSource>().PlayOneShot(soundWoodChop4);
     }
 }    
 
 function Wait()
 {
     yield WaitForSeconds(0.2);
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Best Answer

Answer by Harald921 · May 04, 2015 at 09:28 AM

Answer to my own question, the raycast detected the characters collider. Put the character in a layer that ignores raycasts and it will work!

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Raycast consuming too much ! (VR) 2 Answers

in rewarded ad which code means user closed the ad by itself 2 Answers

Using Ray cast for click to move 2 Answers

Camera switch between child back to main camera issue 2 Answers

Vertical distance indication 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