• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by carter-carl30 · Jun 10, 2013 at 06:16 PM · javascripttexture2dtransform.positionswapmaintexture

Swap texture2D on enemy sprite once reached a certain point

I am trying to make a 2D game, I am using t$$anonymous$$s script, I have 3 empty gameobjects as way points, 1 (where enemy spawns) 2 & 3 w$$anonymous$$ch 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 t$$anonymous$$s 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 t$$anonymous$$s, 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 t$$anonymous$$nking 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 t$$anonymous$$s in code

Comment
Add comment · Show 11
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 AmoralAckbar · Jun 10, 2013 at 08:15 PM 0
Share

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

avatar image carter-carl30 · Jun 10, 2013 at 10:29 PM 0
Share

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"

avatar image AmoralAckbar · Jun 10, 2013 at 11:27 PM 0
Share

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.

avatar image AmoralAckbar · Jun 10, 2013 at 11:45 PM 1
Share

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.

avatar image AmoralAckbar · Jun 10, 2013 at 11:56 PM 1
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AmoralAckbar · Jun 11, 2013 at 12:23 AM

I do my coding in C#, so t$$anonymous$$s 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 somet$$anonymous$$ng that gives the mesh the initial state animation that you are going to want.

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.

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

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

texture2D opacity?(javascript) 0 Answers

Checkpoint Scripting Help 1 Answer

How do I add a vector3.lerp or movetoward 1 Answer

Rotate 2D Texture negative positive degrees touch 2 Answers

Portal doors and exits. Can you set up one script to handle tranporting of the character to the exit? 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