- Home /
Please help in changing a script! Need to add a trigger enter function!
I have a Collect Papers script from alucarj, but I need it changed. Here it is...
#pragma strict
@script RequireComponent( AudioSource )
var papers : int = 0; // counter of how many papers collected
var papersToWin : int = 8; // total number of papers in the scene
var distanceToPaper : float = 5.5; // maximum distance that the raycast will detect
var sphereRadius : float = 1.0; // the width of the sphere that is being SphereCast
public var scaleTimerMax : float = 1.5; // how fast the paper disappears
var paperSound : AudioClip;
var enemyScript : NPCMovement;
var musicManagerScript : MusicManager;
private var currentPaperMaterial : Material;
public var paperDisplayObject : Transform;
public var paperDisplayLight : Light;
public var paperDisplayParticles : ParticleEmitter;
private var startPos : Vector3;
private var startScale : Vector3;
private var particleStartPos : Vector3;
function Start()
{
if ( !enemyScript )
{
Debug.LogWarning( "No Enemy Script in the Inspector" );
var nme : GameObject = GameObject.Find( "Enemy" );
enemyScript = nme.GetComponent( NPCMovement );
}
if ( !musicManagerScript )
{
Debug.LogWarning( "No Music Manager Script in the Inspector" );
var mmngr : GameObject = GameObject.Find( "MusicManager" );
musicManagerScript = mmngr.GetComponent( MusicManager );
}
if ( !paperDisplayObject )
{
Debug.LogWarning( "No paper Display Object in the Inspector" );
paperDisplayObject = GameObject.Find( "PaperDisplayObject" ).transform;
}
if ( !paperDisplayLight )
{
Debug.LogWarning( "No paper Display Light in the Inspector" );
paperDisplayLight = GameObject.Find( "PaperViewSpotlight" ).light;
}
if ( !paperDisplayParticles )
{
Debug.LogWarning( "No paper Display Particles in the Inspector" );
paperDisplayParticles = GameObject.Find( "DustParticleEffect" ).particleEmitter;
}
paperDisplayObject.renderer.enabled = false;
paperDisplayLight.enabled = false;
paperDisplayParticles.emit = false;
particleStartPos = paperDisplayParticles.transform.localPosition;
startPos = paperDisplayObject.localPosition;
startScale = paperDisplayObject.localScale;
}
function Update()
{
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown( KeyCode.E ) )
{
var hit : RaycastHit;
var rayOrigin : Ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0 ) );
if ( Physics.SphereCast( rayOrigin, sphereRadius, hit, distanceToPaper ) )
{
//Debug.Log( "SphereCast Hit : " + hit.collider.gameObject.name );
//Debug.DrawLine( Camera.main.transform.position, hit.point, Color.red, 1.5 );
if ( hit.collider.gameObject.name == "Paper" )
{
//Debug.Log( "SPHERE hit Paper for sure" );
papers += 1;
// play paper has been picked up sound
audio.PlayClipAtPoint( paperSound, hit.point );
// destroy the paper
Destroy( hit.collider.gameObject );
// tell enemy to follow closer
enemyScript.ReduceDistance();
// tell enemy to teleport now (if you want)
enemyScript.TeleportEnemy();
// tell the player they go mad quicker
gameObject.SendMessage( "DecreaseHealthDecayRate", SendMessageOptions.RequireReceiver );
// change the music based on the number of papers collected
if ( papers == 2 )
{
musicManagerScript.PlayMusicTrack( 1 );
}
else if ( papers == 4 )
{
musicManagerScript.PlayMusicTrack( 2 );
}
else if ( papers == 6 )
{
musicManagerScript.PlayMusicTrack( 3 );
}
else if ( papers == papersToWin )
{
Debug.Log( "You have collected All Papers !" );
// load Win Scene here !!!!
Application.LoadLevel( "SceneWin" );
}
else if ( papers == 0 )
{
musicManagerScript.PlayMusicTrack( 0 );
}
// set current paper material
currentPaperMaterial = hit.collider.gameObject.renderer.material;
DisplayPaper();
}
}
}
}
function DisplayPaper()
{
// reset the size
paperDisplayObject.localPosition = startPos;
paperDisplayObject.localScale = startScale;
paperDisplayParticles.transform.localPosition = particleStartPos;
// --
paperDisplayObject.renderer.material = currentPaperMaterial;
paperDisplayObject.renderer.enabled = true;
paperDisplayLight.enabled = true;
paperDisplayParticles.emit = true;
// --
var scaleTimer : float = 0.0;
//var scaleTimerMax : float = 2.5;
var isScaling : boolean = true;
while ( isScaling )
{
scaleTimer += Time.deltaTime;
if ( scaleTimer > scaleTimerMax )
{
isScaling = false;
}
var scaleFactor : float = ( scaleTimerMax - scaleTimer ) / scaleTimerMax;
paperDisplayObject.localScale.y = startScale.y * scaleFactor;
// setting the scale
var newPosY : float = startPos.y + ( ( startScale.y * 0.5 ) - ( paperDisplayObject.localScale.y * 0.5 ) );
// adjusting the position
paperDisplayObject.localPosition.y = newPosY;
paperDisplayParticles.transform.localPosition.y = particleStartPos.y + ( startScale.y - paperDisplayObject.localScale.y );
// retile the texture
currentPaperMaterial.mainTextureScale = Vector2( 1.0, scaleFactor ); // change the scaling of the texture
currentPaperMaterial.SetTextureOffset( "_MainTex", Vector2( 0.0, 1.0 - scaleFactor ) ); // change the UV positions of the texture
// next frame
yield;
}
// --
// effect is over,
// reset the light and renderer
paperDisplayObject.renderer.enabled = false;
paperDisplayLight.enabled = false;
paperDisplayParticles.emit = false;
currentPaperMaterial.mainTextureScale = Vector2.one;
currentPaperMaterial.SetTextureOffset( "_MainTex", Vector2.zero );
In the function Update the third line is an if statement, but I don't want it to work after a mouse click. I need it to get enabled after colliding a trigger collider on a paper. But I'm quite new to scripting, trying to do it myself just won't work. Help please!
Just removing that if statement should give you your desired interaction. The next check is for whether the paper is within a spherecast of the user. Its probably not the most efficient way to handle it, but it should do what you are asking, with $$anonymous$$imal effort
So I understand correctly; When the player touches the paper you want to destroy the object and add it to the player's inventory or whatever?
I want to save the idea of the script, but to collect the paper not after a mouse click, but after a collision
Answer by Ben-Stoneman · Aug 15, 2013 at 01:36 PM
Add this Function:
public var target : GameObject;
function OnTriggerEnter(other : Collider)
{
Debug.Log("Trigger Entered");
if (other != target)
{
Debug.Log("Not the player");
}
else Destroy(gameObject);
//Increase a variable to keep track of score
}
Remember to:
Add the player game object to the script in the inspector.
Add a sphere collider to the note and tick the "Is Trigger" checkbox
Check the following:
The target GameObject has a Rigid body.
The player object has been add to the script's game object slot in the inspector
If your player is instantiated, you will need to add it to the public gameobject "target" using Resources.Load.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
Triggering platform animation on colliding with Button 0 Answers
Use trigger with player but have a collider with everything else? 3 Answers
How do you go from one scene, to another, then a third and back again ? 0 Answers
Stop object colliders (non-mesh) bouncing on collision 1 Answer