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 Aethryx_Two · Aug 31, 2014 at 03:49 PM · raycastbeginnerraycasthitenemies

Using raycasters to freeze enemies

I found out about raycasts recently, and I want to try and use one to make the player's phone light freeze enemies in place when it touches them by casting a ray from the phone that sends a message to enemies when it touches them to stop all movement and animation until the ray isn't touching them anymore. The problem is I have almost no experience with Javascript or C#, so I have no idea what I'm doing S: I've looked at plenty of tutorials, but none have exactly what I need. Would anyone be able to help, maybe come up with a raycasting script I could go off of?

This is all I have for my raycast script.

 #pragma strict
 
 private var lineTransform : Vector3;
 private var startTransform : Vector3;
 
 function Start ()
 {
     lineTransform = transform.position;
     startTransform = transform.position;
 }
 
 function Update () {
 
 var hit : RaycastHit;
 var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5,0));
     if (Physics.Raycast (ray, hit, 100)) {
     Debug.DrawLine (ray.origin, hit.point);
     }
         //Debug.DrawRay(startTransform, lineTransform, Color.red);
 }



There's not really anything there, except a blank ray.

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 aldonaletto · Aug 31, 2014 at 04:15 PM

You won't see anything because rays originate at the camera, what reduces them to a single point in the 2D screen - at most a single pixel will be lit at the center of the screen, in this case.

You could try something like this instead:

 ...
 if (Physics.Raycast (ray, hit, 100)) {
     print("Freeze, "+hit.transform.name);
 }

This will print "Freeze" + target's name whenever your ray hits some object. If it's ok, you can go a step further by replacing this instruction with a SendMessage:

 if (Physics.Raycast (ray, hit, 100)) {
     hit.transform.SendMessage("Freeze", SendMessageOptions.DontRequireReceiver);
 }
     

This code will try to call the function Freeze() in any script attached to the target (does nothing if such function doesn't exist).

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 Aethryx_Two · Aug 31, 2014 at 06:21 PM 0
Share

Thanks for the answer :) Unfortunately, when I tried adding the script in, nothing seemed to happen. It was also trying to freeze everything it touched, rather than just the enemy.

avatar image aldonaletto · Sep 02, 2014 at 01:49 PM 0
Share

Send$$anonymous$$essage actually tries to call a function by name in the target object - in this case, the object you hit with a Raycast. You must create a function called Freeze() in some script, and attach this script to the targets you want to freeze - only the ones that have that script will respond to the raycast. Just to test this, create a script like the one below and attach it to some enemy:

 function Freeze(){
   print("I was hit");
 }

When you point your camera to an enemy, it will print "I was hit" continuously while it's being hit.

You should also do the raycast only when you want to freeze something: if it's a touch device, place the Raycast code inside the touch routine - like this:

 function Update(){
   if (Input.touchCount > 0){ // if there's any touch...
     // do the Raycast
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5,0));
     if (Physics.Raycast (ray, hit, 100)) {
       hit.transform.Send$$anonymous$$essage("Freeze", Send$$anonymous$$essageOptions.DontRequireReceiver);
     }
   }
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast Coding Issues 1 Answer

Raycast Hit not toggling variable if stops hitting 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Click to move script help 1 Answer

Raycast problems! 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