• 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
-2
Question by jazza · Aug 04, 2012 at 11:04 AM · gameobjectaudiocustom

How to make a Slender man follow character script

hi guys i was just wondering if anybody new how to make a script so that the 'slenderman' teleports to positions near the player, and gets closer and closer, sorry for the big ask but could anyone help me out?

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 DESTRUKTORR · Aug 06, 2012 at 04:28 PM 4
Share

This is a little bit too much to be asking, I think. If you want to grow, as a programmer, you need to learn some basic problem solving, and try something on your own, first.

Just take a step back and try to figure out what you need to do, overall, then come back and ask questions if your code isn't working right. Good luck!

avatar image jazza · Aug 08, 2012 at 01:20 PM 0
Share

kay, thanks!

avatar image babyjay521 · Feb 11, 2013 at 02:59 AM 1
Share

The basic idea is getting the player's position, and changing the slenderman's position in relation to it. Also, you could use RayCasts to find trees and other items that the slenderman could hide behind, and position him so he is there, etc. But like DESTRU$$anonymous$$TORR said, you will really grow as a programmer if you take on this project by yourself. Trust me, it is much more satisfying to see your own program work than to copy and paste someone else's into your game. Also, you will have a better understanding of the program, and you'll be able to make it to best fit you needs. Hope everything works out well for you! EDIT: Just realized how old this was, sorry to bring it back up haha.

avatar image Unitraxx · Apr 04, 2013 at 12:31 AM 0
Share

I actually can't believe it got -6, it's a well-formed question. Ok maybe not a good question for on UAnswers but there's some real rubbish out there that doesn't even get -1.

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HSMInteractive · Aug 01, 2013 at 09:08 PM

 #pragma strict
 

// | Credit to Alucard Jay

 @script RequireComponent( Rigidbody )
 
 private var myTransform : Transform;
 private var myRigidbody : Rigidbody;
 private var desiredVelocity : Vector3;
 
 var isGrounded : boolean = false;
 
 var target : Transform;
 
 var moveSpeed : float = 6.0;
 var turnSpeed : float = 2.0;
 
 var hitNormal : float = 20.0;
 
 var shoulderMultiplier : float = 0.5;
 
 var rayDistance : float = 5.0;
 
 enum NPC
 {
     Idle,
     FreeRoam,
     Chasing,
     RunningAway
 }
 
 var myState : NPC;
 
 var minimumRange : float = 4.0;
 var maximumRange : float = 45.0;
 
 private var minimumRangeSqr : float;
 private var maximumRangeSqr : float;
 
 var terrainMaximumHeight : float = 5000.0;
 
 var isNpcChasing : boolean = true;
 
 var freeRoamTimer : float = 0.0;
 var freeRoamTimerMax : float = 5.0;
 var freeRoamTimerMaxRange : float = 1.5;
 var freeRoamTimerMaxAdjusted : float = 5.0;
 
 var calcDir : Vector3;
 
 var isSlender  : boolean = true;
 
 function Start()
 {
 
     myTransform = transform;
     myRigidbody = rigidbody;
     
     minimumRangeSqr = minimumRange * minimumRange;
     maximumRangeSqr = maximumRange * maximumRange;
     
     rigidbody.freezeRotation = true;
     
     //calcDir = Random.onUnitSphere;
     //calcDir.y = myTransform.position.y;
     
     var freeRoamTimer : float = 1000;
     
     if ( isSlender )
     {
         InvokeRepeating( "TeleportEnemy", 60.0, 20.0 );
     }
     
 }
 
 function TeleportEnemy()
 {
     CheckIfVisible();
     
     if ( !isVisible )
     {
         var  sqrDist : float = ( target.position - myTransform.position ).sqrMagnitude;
         
         if ( sqrDist > maximumRangeSqr + 25.0 )
         {
             
             var teleportDistance : float = maximumRange + 5.0;
             
             var rndDir : int = Random.Range( 0, 2 );
             
             if ( rndDir == 0 )
             {
                 rndDir = -1;
             }
             
             //var terrainPosCheck : Vector3 = target.position + ( myTransform.right * teleportDistance ); 
             var terrainPosCheck : Vector3 = target.position + ( rndDir * target.right * teleportDistance ); 
             
             terrainPosCheck.y = terrainMaximumHeight;
             
             var hit : RaycastHit;
             
             if ( Physics.Raycast( terrainPosCheck, -Vector3.up, hit, Mathf.Infinity ) )
             {
                 if ( hit.collider.gameObject.tag == "Ground" || hit.collider.gameObject.name == "Terrain" )
                 {
                     myTransform.position = hit.point + new Vector3( 0, 0.25, 0 );
                 }
             }
         }
     }
 }
 
 var isVisible : boolean = false;
 
 var offScreenDot : float = 0.8;
 
 function Slender()
 {
     CheckIfVisible();
     
     var  sqrDist : float = ( target.position - myTransform.position ).sqrMagnitude;
     
     if ( isVisible )
     {
         // Check the range
         if ( sqrDist > maximumRangeSqr )
         {
             myState = NPC.Chasing;
         }
         else
         {
         
             var hit : RaycastHit;
         
             if ( Physics.Linecast( myTransform.position, target.position, hit ) )
             {
                 
                 //Debug.Log( hit.collider.gameObject.name );
                 
                 Debug.DrawLine( myTransform.position, target.position, Color.blue );
             
                 if ( hit.collider.gameObject.name == target.name )
                 {
                     myState = NPC.Idle;
             
                     //decrease health here        
                 }
                 else
                 {
                     myState = NPC.Chasing;
                 }
             }
         }
         
     }
     else // is NOT visible
     {
          if ( sqrDist > minimumRangeSqr )
          {
              myState = NPC.Chasing;
          }
          else
          {
              myState = NPC.Idle;
          }
     }    
     
 }
 
 function CheckIfVisible()
 {
     var fwd : Vector3 = target.forward;
     var other : Vector3 = ( myTransform.position - target.position ).normalized;
     //var other : Vector3 = ( target.position - myTransform.position ).normalized;
     
     var dotProduct : float = Vector3.Dot( fwd, other );
     
     if ( dotProduct > offScreenDot )
     {
         isVisible = true;
     }
     else
     {
         isVisible = false;
     }
     
 }
 
 function MakeSomeDecisions()
 {
     var  sqrDist : float = ( target.position - myTransform.position ).sqrMagnitude;
     
     //STATE IF STATMENTS
     if ( sqrDist > maximumRangeSqr )
     {
         if ( isNpcChasing )
         {
             myState = NPC.Chasing;
         }
         else
         {
             myState = NPC.FreeRoam;
         }
     }
     else if ( sqrDist < minimumRangeSqr )
     {
         if ( isNpcChasing )
         {
             myState = NPC.Idle;
         }
         else
         {
             myState = NPC.RunningAway;
         }
     }
     else
     {
         if ( isNpcChasing )
         {
             myState = NPC.Chasing;
         }
         else
         {
             myState = NPC.RunningAway;
         }
     }
 }
 
 function Update()
 {
     if ( isSlender )
     {
         Slender();
     }
     else
     {
         MakeSomeDecisions();
     }
     
     switch( myState )
     {
         case NPC.Idle :
             myTransform.LookAt( target );
             desiredVelocity = new Vector3( 0, myRigidbody.velocity.y, 0 );
         break;
         
         case NPC.FreeRoam :
             freeRoamTimer += Time.deltaTime;
             
             if ( freeRoamTimer > freeRoamTimerMaxAdjusted )
             {
                 freeRoamTimer = 0.0;
                 freeRoamTimerMaxAdjusted = freeRoamTimerMax + Random.Range( -freeRoamTimerMaxRange, freeRoamTimerMaxRange );
                 
                 calcDir = Random.onUnitSphere;
                 calcDir.y = 0.0; //myTransform.position.forward.y;
             }
             
             Moving( calcDir );
             
             //desiredVelocity = new Vector3( 0, myRigidbody.velocity.y, 0 );
         break;
         
         case NPC.Chasing :
             Moving( (target.position - myTransform.position).normalized );
         break;
         
         case NPC.RunningAway :
             Moving( (myTransform.position - target.position).normalized );
         break;
     }
 
 
     //Moving();
 }
 
 function Moving( lookDirection : Vector3)
 {
     //rotation 
     //var lookDirection : Vector3 = (target.position - myTransform.position).normalized;
     
     var hit : RaycastHit;
     
     
     
     var leftRayPos : Vector3 = myTransform.position - ( myTransform.right * shoulderMultiplier );
     var rightRayPos : Vector3 = myTransform.position + (myTransform.right * shoulderMultiplier );
     
     if ( Physics.Raycast( leftRayPos, myTransform.forward,  hit, rayDistance )  )
     {
         if ( hit.collider.gameObject.tag != "Ground" )
         {
             Debug.DrawLine( leftRayPos, hit.point, Color.red );
         
             lookDirection += hit.normal * hitNormal;
         }
     }
     else if ( Physics.Raycast( rightRayPos, myTransform.forward,  hit, rayDistance )  )
     {
         if ( hit.collider.gameObject.tag != "Ground" )
         {
             Debug.DrawLine( rightRayPos, hit.point, Color.red );
         
             lookDirection += hit.normal * hitNormal;
         }
     }
     else
     {
         Debug.DrawRay( leftRayPos, myTransform.forward * rayDistance, Color.green );
         Debug.DrawRay( rightRayPos, myTransform.forward * rayDistance, Color.green );
     }
     
     var lookRot : Quaternion = Quaternion.LookRotation ( lookDirection );
     
     myTransform.rotation = Quaternion.Slerp( myTransform.rotation, lookRot, turnSpeed * Time.deltaTime );
     
     //movement
     //myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     desiredVelocity = myTransform.forward * moveSpeed;
     desiredVelocity.y = myRigidbody.velocity.y;
 }
 
 function FixedUpdate()
 {
     if ( isGrounded )
     {
         myRigidbody.velocity = desiredVelocity;
     }
 }
 
 function OnCollisionEnter ( collision : Collision )
 {
     if ( collision.collider.gameObject.tag == "Ground" || collision.collider.gameObject.name == "Terrain" )
     {
         isGrounded = true;
     }
 }
 
 function OnCollisionStay ( collision : Collision )
 {
     if ( collision.collider.gameObject.tag == "Ground" || collision.collider.gameObject.name == "Terrain")
     {
         isGrounded = true;
     }
 }
 
 function OnCollisionExit ( collision : Collision ) 
 {
     if ( collision.collider.gameObject.tag == "Ground" || collision.collider.gameObject.name == "Terrain")
     {
         isGrounded = false;
     }
 }


Add a capsule collider to your camera about the same size as your first person controller. Then drag the camera to the "Target" variable. Set the rest of the values to your liking. Terrain maximum height is the max height your terrain reaches so set that too.

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
  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

48 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 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 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

Slender like game 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GameObject reacts to audio source 1 Answer

Music works but not when accessed by another script in JavaScript 1 Answer

Get acess and switch to many audio source in a single Object HELP!! 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