- Home /
How do I make a 2D character/npc follow the player using texture offsetting
What I am trying to achieve is making an extra character in my game follow the player (my game is a 2.5D game, so the character is billboarding the camera), but when i use the script he doesn't change his animation and he keeps trying to push the player if he is touching him. I also would want for the character to follow directly behind the player (just like in the game The legend of Zelda: Four Swords Adventure) rather to the sides/front of the player. Any idea how I would script this, or are there any tutorials covering this topic that someone can direct me to, because I already searched on google and couldn't find anything. Thanks!
And heres the current script I attached to the character, which probably needs to be re-written completely lol
 #pragma strict
 var player : Transform;
 var speed = 4;
 var left : Texture;
 var right : Texture;
 var up : Texture;
 var back : Texture;
 var xSpeed = 0;
 var ySpeed = 0;
 function Start () {
 
 }
 
 
 
 function Update(){
     
 if(player.position.x < transform.position.x){
 transform.position += Vector3.left * Time.deltaTime * speed;
 renderer.material.SetTexture("_MainTex", left);
 }
 if(player.position.x > transform.position.x){
 transform.position += Vector3.right * Time.deltaTime * speed;
 renderer.material.SetTexture("_MainTex", right);
 }
 if(player.position.z < transform.position.z){
 transform.position += Vector3.forward * Time.deltaTime * -speed;
 renderer.material.SetTexture("_MainTex", up);
 }
 if(player.position.z > transform.position.z){
 transform.position += Vector3.forward * Time.deltaTime * speed;
 renderer.material.SetTexture("_MainTex", back);
 }
 
 }
 function OnCollisionEnter(other : Collision) 
 { 
     if (other.gameObject.tag == "Player")
     {
     speed = 0;
     }
 }
 function OnCollisionExit(other : Collision) 
 { 
     if (other.gameObject.tag == "Player") {
     speed = 4;
     }
     }
     
     
 var uvAnimationTileX = 24; //Here you can place the number of columns of your sheet.
 //The above sheet has 24
 
 var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.
 //The above sheet has 1
 var framesPerSecond = 0.0;
 
 function LateUpdate () {
 
 // Calculate index
 var index : int = Time.time * framesPerSecond;
 // repeat when exhausting all frames
 index = index % (uvAnimationTileX * uvAnimationTileY);
 
 // Size of every tile
 var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
 
 // split into horizontal and vertical index
 var uIndex = index % uvAnimationTileX;
 var vIndex = index / uvAnimationTileX;
 
 // build offset
 // v coordinate is the bottom of the image in opengl so we need to invert.
 var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
 renderer.material.SetTextureOffset ("_MainTex", offset);
 renderer.material.SetTextureScale ("_MainTex", size);
 }
I got the 2d animation script from the unity wiki : http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture
Also, I attached my spritesheet to a cube which is billboarding the camera. Is there anyway to make the texture billboard the camera rather than the cube, because im having a hard time setting the colliders for the cube since its billboarding the camera. thanks again.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                