- Home /
Cant pickup papers with script
I cant pickup papers with script , with "E" , and i dont know why.
#pragma strict
@script RequireComponent( AudioSource )
var papers : int = 0;
var papersToWin : int = 8;
var distanceToPaper : float = 2.5;
public var paperPickup : AudioClip;
var theEnemy : EnemyScript;
function Start()
{
Screen.lockCursor = true;
// find and store a reference to the enemy script (to reduce distance after each paper is collected)
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
}
}
function Update()
{
//if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
{
//var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPaper ) )
{
//if ( hit.collider.gameObject.tag == "Paper" )
if ( hit.collider.gameObject.name == "Paper" )
{
papers += 1;
//Debug.Log( "A paper was picked up. Total papers = " + papers );
audio.PlayClipAtPoint( paperPickup, transform.position );
Destroy( hit.collider.gameObject );
// make enemy follow closer
theEnemy.ReduceDistance();
}
}
}
}
function OnGUI()
{
if ( papers < papersToWin )
{
GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + papers.ToString() + " Papers Collected" );
}
else
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
// Application.LoadLevel( "sceneWin" );
}
}
Nice to see some originality. Normally the picking up papers script requires 5 to win...
I don't see anything wrong with the code... $$anonymous$$ight be missing something though. Do you have any errors in the console?
Put debug.log everywhere. You need to check if your input is working, if your raycast is returning true and if your collider name check is working.
BTW, tag checking is normally preferred over name checking.
ARE YOUR OBJECTS NA$$anonymous$$ED PAPER ??!!
if I had a dollar for everytime....
Add this at line 37 :
Debug.Log( "Raycast hit " + hit.collider.gameObject.name );
This will print in the console if/what your raycast hit.
and also try increasing the length of the raycast (change distanceToPaper to a higher number).
Answer by ExplodingCookie · Jul 17, 2014 at 10:50 PM
,To answer your question. Try using this in place of your reference to the key.
Input.GetKeyDown("e")
Hopefully this solves your issue. :)
~ExplodingCookie
I'm aware they will do the same thing in theory. But using this always works for me. The best way to solve a problem is to tinker around with things.
Answer by Kiwasi · Jul 18, 2014 at 03:10 AM
Here is a debugged version of your update function. You should see all of the messages in the console. I suggest commenting each one out once you have seen it, messaging the console will slow the game down.
This will allow you to isolate which line is not working. Post back once you have figured it out.
function Update()
{
Debug.Log("Update is working");
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
{
Debug.Log("Inut is working");
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPaper ) )
{
Debug.Log("Raycast is working");
if ( hit.collider.gameObject.name == "Paper" )
{
Debug.Log("Name checking is working");
papers += 1;
audio.PlayClipAtPoint( paperPickup, transform.position );
Destroy( hit.collider.gameObject );
theEnemy.ReduceDistance();
}
}
}
}
Now we are getting somewhere. The problem is in your raycast. Let me have a look at that a little more closely and get back to you.
Try adding the following line immediately after your var ray.
Debug.DrawRay(ray.origin, ray.direction * distanceToPaper, Color.white, 1, false);
This should draw the line in your scene view. Is this line co$$anonymous$$g anywhere near your paper collider?
Now the code is this:
#pragma strict
@script RequireComponent( AudioSource )
var papers : int = 0;
var papersToWin : int = 3;
var distanceToPaper : float = 5.5;
public var paperPickup : AudioClip;
var theEnemy : EnemyScript;
function Start()
{
Screen.lockCursor = true;
// find and store a reference to the enemy script (to reduce distance after each paper is collected)
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
}
}
function Update()
{
Debug.Log("Update is working");
if ( Input.Get$$anonymous$$ouseButtonDown(0) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) )
{
Debug.Log("Inut is working");
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
Debug.DrawRay(ray.origin, ray.direction * distanceToPaper, Color.white, 1, false);
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPaper ) )
{
Debug.Log("Raycast is working");
if ( hit.collider.gameObject.name == "Paper" )
{
Debug.Log("Name checking is working");
papers += 1;
audio.PlayClipAtPoint( paperPickup, transform.position );
Destroy( hit.collider.gameObject );
theEnemy.ReduceDistance();
}
}
}
}
function OnGUI()
{
if ( papers < papersToWin )
{
GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + papers.ToString() + " Papers Collected" );
}
else
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
// Application.LoadLevel( "sceneWin" );
}
}
The debug doesnt make any line
I think the problem is here: if ( Physics.Raycast( ray, hit, distanceToPaper ) )