- Home /
Swap texture2D on enemy sprite once reached a certain point
I am trying to make a 2D game, I am using this script, I have 3 empty gameobjects as way points, 1 (where enemy spawns) 2 & 3 which it moves to and fro in a loop:
var waypoint : Transform[];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = false;
var Distance : float = 1;
function Awake(){
} function Update () {
if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < Distance)
{
currentWaypoint++;
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop)
{
currentWaypoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
}
I am using this script to animate the texture2D:
var numFrames : int = 3;
var curFrame : int = 1;
function Update(){
transform.position.z = 0.01;
renderer.material.mainTextureOffset.x += 1.0 / numFrames + Time.timeScale;
curFrame += 1;
if (curFrame > numFrames){
renderer.material.mainTextureOffset.x = 0;
curFrame = 1;
}
}
What I am trying to do is have the texture2D change when the enemy is facing left or right (currently just looks correct one way then moonwalks back lol) I have tried to make a script to do this, here it is below:
public var walkright : Texture2D; //walking right
public var walkleft : Texture2D; //walking left
public var mesh : GameObject; //Mesh
function Update () {
if (mesh.transform.position.x < (5.097385)){
mesh.renderer.material.mainTexture = walkleft;
}
if (mesh.transform.position.x > (-4.12935)){
mesh.renderer.material.mainTexture = walkright;
}
}
can anyone help me/suggest where I am going wrong? I'm thinking I need to say if distance from waypoint 2 is decreasing use texture left and same for waypoint 3 but I don't know how to type this in code
What are you trying to do with that code? Is that supposed to be "greater than or equal to" and "less than or equal to"? If so, I think that is supposed to be >= and <=. If that is not what you are trying to do, I just have never seen that syntax...
see updated script and question the values in the brackets are waypoint 2 & 3 x positions but I think what im saying there is the same thing! I just don't know how to say "if moving away from waypoint 2 use left texture and if moving away from waypoint 3 use right texture"
I think that a general solution that would probably work is to just save off the old position in a private member variable in the update. Then each call to update you can check the new position against that old position and update the animation accordingly based on if the change has been left or right. After that logic save the new position as the old position and repeat. This way the animation will be accurate no matter where your waypoints are.
I do my coding in C#, so this might not be 100% syntax accurate:
public var walkright : Texture2D; //walking right
public var walkleft : Texture2D; //walking left
public var mesh : GameObject; //$$anonymous$$esh
private var oldXPos : float; //Old x position
function Update () {
if (mesh.transform.position.x >= oldXPos)
{
mesh.renderer.material.mainTexture = walkright;
}
else
{
mesh.renderer.material.mainTexture = walkleft;
}
oldXPos = mesh.transform.position.x;
}
You will probably want to initialize oldXPos to something that gives the mesh the initial state animation that you are going to want.
No, I was just saying that oldXPos will need some initial starting spot. If your enemy generally starts at ~5 and he is going to start by moving to the right, you could set your oldXPos to 0 for example. This way when update runs, the "walk right" texture will be set because mesh.transform.position.x will be greater than oldXPos. After that, the oldXPos will be getting set with every Update call, so you should be good.
Answer by AmoralAckbar · Jun 11, 2013 at 12:23 AM
I do my coding in C#, so this might not be 100% syntax accurate:
public var walkright : Texture2D; //walking right
public var walkleft : Texture2D; //walking left
public var mesh : GameObject; //Mesh
private var oldXPos : float; //Old x position
function Update () {
if (mesh.transform.position.x >= oldXPos)
{
mesh.renderer.material.mainTexture = walkright;
}
else
{
mesh.renderer.material.mainTexture = walkleft;
}
oldXPos = mesh.transform.position.x;
}
You will probably want to initialize oldXPos to something that gives the mesh the initial state animation that you are going to want.