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 CYVISUALEFFECTS · Dec 04, 2012 at 10:32 PM · javascript3dbuggamecolliders

Slenderman script problem

I am making a slenderman game in unity pro 4. Everything works fine except from 2. The follow script and the tree colliders. 1)The player won't collide with trees. Tree colliders are on. Don't know how to fix that:( 2)The script to make him follow is a bit buggy. Works good, but instead of stay, he flies in the air a meter from the ground in horizontal position. I don't know exactly where the problem is. Hope you help me:/ Here is it: #pragma strict @script RequireComponent( AudioSource )

 public var thePlayer : Transform;
 private var theEnemy : Transform;
 
 public var speed : float = 5.0;
 
 var isOffScreen : boolean = false;
 public var offscreenDotRange : float = 0.7;
 
 var isVisible : boolean = false;
 public var visibleDotRange : float = 0.8; // ** between 0.75 and 0.85 (originally 0.8172719) 
 
 var isInRange : boolean = false;
 
 public var followDistance : float = 24.0;
 public var maxVisibleDistance : float = 25.0;
 
 public var reduceDistAmt : float = 3.1;
 
 private var sqrDist : float = 0.0;
 
 public var health : float = 100.0;
 public var damage : float = 20.0;
 
 public var enemySightedSFX : AudioClip;
 
 private var hasPlayedSeenSound : boolean = false;
 
 private var colDist : float = 5.0; // raycast distance in front of enemy when checking for obstacles
 
 
 function Start() 
 {
     if ( thePlayer == null )
     {
        thePlayer = GameObject.Find( "Player" ).transform;
     }
 
     theEnemy = transform;
 }
 
 function Update() 
 {
     // Movement : check if out-of-view, then move
     CheckIfOffScreen();
 
     // if is Off Screen, move
     if ( isOffScreen )
     {
        MoveEnemy();
 
        // restore health
        RestoreHealth();
     }
     else
     {
        // check if Player is seen
        CheckIfVisible();
 
        if ( isVisible )
        {
          // deduct health
          DeductHealth();
 
          // stop moving
          StopEnemy();
 
          // play sound only when the Man is first sighted
          if ( !hasPlayedSeenSound )
          {
           audio.PlayClipAtPoint( enemySightedSFX, thePlayer.position ); 
          }
          hasPlayedSeenSound = true; // sound has now played
        }
        else
        {
          // check max range
          CheckMaxVisibleRange();
 
          // if far away then move, else stop
          if ( !isInRange )
          {
           MoveEnemy();
          }
          else
          {
           StopEnemy();
          }
 
          // reset hasPlayedSeenSound for next time isVisible first occurs
          hasPlayedSeenSound = false;
        }
     }
 
 }
 
 
 function DeductHealth() 
 {
     // deduct health
     health -= damage * Time.deltaTime;
 
     // check if no health left
     if ( health <= 0.0 )
     {
        health = 0.0;
        Debug.Log( "YOU ARE OUT OF HEALTH !" );
 
        // Restart game here!
        // Application.LoadLevel( "sceneLose" );
     }
 }
 
 
 function RestoreHealth() 
 {
     // deduct health
     health += damage * Time.deltaTime;
 
     // check if no health left
     if ( health >= 100.0 )
     {
        health = 100.0;
        //Debug.Log( "HEALTH is FULL" );
     }
 }
 
 
 function CheckIfOffScreen() 
 {
     var fwd : Vector3 = thePlayer.forward.normalized;
     var other : Vector3 = (theEnemy.position - thePlayer.position).normalized;
 
     var theProduct : float = Vector3.Dot( fwd, other );
 
     if ( theProduct < offscreenDotRange )
     {
        isOffScreen = true;
     }
     else
     {
        isOffScreen = false;
     }
 }
 
 
 function MoveEnemy() 
 {
     // Check the Follow Distance
     CheckDistance();
 
     // if not too close, move
     if ( !isInRange )
     {
        rigidbody.velocity = Vector3( 0, rigidbody.velocity.y, 0 ); // maintain gravity
 
        // --
        // Old Movement
        //transform.LookAt( thePlayer );   
        //transform.position += transform.forward * speed * Time.deltaTime;
        // --
 
        // New Movement - with obstacle avoidance
        var dir : Vector3 = ( thePlayer.position - theEnemy.position ).normalized;
        var hit : RaycastHit;
 
        if ( Physics.Raycast( theEnemy.position, theEnemy.forward, hit, colDist ) )
        {
          //Debug.Log( " obstacle ray hit " + hit.collider.gameObject.name );
          if ( hit.collider.gameObject.name != "Player" && hit.collider.gameObject.name != "Terrain" )
          {         
           dir += hit.normal * 20;
          }
        }
 
        var rot : Quaternion = Quaternion.LookRotation( dir );
 
        theEnemy.rotation = Quaternion.Slerp( theEnemy.rotation, rot, Time.deltaTime );
        theEnemy.position += theEnemy.forward * speed * Time.deltaTime;
 
        // --
     }
     else
     {
        StopEnemy();
     }
 }
 
 
 function StopEnemy() 
 {
     transform.LookAt( thePlayer );
 
     rigidbody.velocity = Vector3.zero;
 }
 
 
 function CheckIfVisible() 
 {
     var fwd : Vector3 = thePlayer.forward.normalized;
     var other : Vector3 = ( theEnemy.position - thePlayer.position ).normalized;
 
     var theProduct : float = Vector3.Dot( fwd, other );
 
     if ( theProduct > visibleDotRange )
     {
        // Check the Max Distance
        CheckMaxVisibleRange();
 
        if ( isInRange )
        {
          // Linecast to check for occlusion
          var hit : RaycastHit;
 
          if ( Physics.Linecast( theEnemy.position + (Vector3.up * 1.75) + theEnemy.forward, thePlayer.position, hit ) )
          {
           Debug.Log( "Enemy sees " + hit.collider.gameObject.name );
 
           if ( hit.collider.gameObject.name == "Player" )
           {
               isVisible = true;
           }
          }
        }
        else
        {
          isVisible = false;
        }
     }
     else
     {
        isVisible = false;
     }
 }
 
 
 function CheckDistance() 
 {
     var sqrDist : float = (theEnemy.position - thePlayer.position).sqrMagnitude;
     var sqrFollowDist : float = followDistance * followDistance;
 
     if ( sqrDist < sqrFollowDist )
     {
        isInRange = true;
     }
     else
     {
        isInRange = false;
     }  
 }
 
 
 function ReduceDistance() 
 {
     followDistance -= reduceDistAmt;
 }
 
 
 function CheckMaxVisibleRange() 
 {
     var sqrDist : float = (theEnemy.position - thePlayer.position).sqrMagnitude;
     var sqrMaxDist : float = maxVisibleDistance * maxVisibleDistance;
 
     if ( sqrDist < sqrMaxDist )
     {
        isInRange = true;
     }
     else
     {
        isInRange = false;
     }  
 }
 
 
 function OnGUI()
 {
     GUI.Box( Rect( (Screen.width * 0.5) - 60, Screen.height - 35, 120, 25 ), "Health : " + parseInt( health ).ToString() );
 }
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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by oopoe · Dec 05, 2012 at 12:50 AM

I'm pretty new to this Unity thing, but I cannot for the life of me get colliding trees when using the terrain tool.

The only way I can get 'solid' trees is by importing a tree model, adding a collider (sphere or capsule) and placing it within. Make this a prefab and then make a ton of copies of the tree.

As for the following thing, I can't help you there, someone else can I am sure.

Comment
Add comment · 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
0

Answer by BLOODragonGames · Dec 05, 2012 at 12:49 AM

For the trees, if you used Terrain to create them, they will not have any ability to collide (yes, even if you add the script). If you want to mass produce trees, use the tree creator, make a tree that you like and then just keep duplicating and moving the duplicates.

As for your Slenderman script, it could have something to do with your ragdoll's rotation position, or it might not. Try and play with its position a bit and see if that's the issue.

Comment
Add comment · Show 1 · 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 CYVISUALEFFECTS · Dec 05, 2012 at 12:19 PM 0
Share

No the issue isnt the ragdoll

avatar image
0

Answer by BZS · Dec 05, 2012 at 12:49 AM

i had this problem too, here is how you solve it 1.create a new prefab 2.find the old tree prefab that has no collider 3.drag the old prefab into the new one 4.gameobject>create other> capsule 5.resize it to your liking 6.now click your terrain 7.paint tree 8.drag your new prefab into your trees 9.you are done!

Comment
Add comment · 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
0

Answer by elJoaco182 · Jan 13, 2013 at 10:49 PM

you have to add a capsule collider to slenderman and drag the enemy(object) to the enemy script

Comment
Add comment · 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
0

Answer by Rapfan28 · Apr 09, 2015 at 04:29 AM

you took that script from alucard jay; without giving credit.

Comment
Add comment · 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

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

15 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

Related Questions

Bugs in my game 1 Answer

Make a game in C# 0 Answers

How to detect when two gameObjects collide by name {java, 2d} 2 Answers

Camera culling problem 3D 0 Answers

Unity 3D Terrain Tree Rotation Bug 0 Answers


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