- Home /
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.
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).
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.
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
Follow this Question
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