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 Lemoln · Aug 15, 2013 at 12:35 PM · collidertriggercollect

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!

Comment
Add comment · Show 4
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 roojerry · Aug 15, 2013 at 12:39 PM 0
Share

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

avatar image Lemoln · Aug 15, 2013 at 12:48 PM 0
Share

It didn't work...

avatar image Ben-Stoneman ♦♦ · Aug 15, 2013 at 12:55 PM 0
Share

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?

avatar image Lemoln · Aug 15, 2013 at 12:57 PM 0
Share

I want to save the idea of the script, but to collect the paper not after a mouse click, but after a collision

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

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 Lemoln · Aug 15, 2013 at 05:36 PM 0
Share

It doesn't work... Says - "Not the player" in debug log

avatar image Ben-Stoneman ♦♦ · Aug 16, 2013 at 12:37 PM 0
Share

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

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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