• 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 AndrewBrown123 · Mar 18, 2020 at 06:07 PM · spriterenderer2d sprites

AI Sprite will not change

I have AI's wondering around my 2D game but they will not change sprites. I have 3 sprites in an array. One looking left, right, and looking strait forward. What needs to happen is the AI moves left the sprite looks left, moves right looks right and so forth.

Here is my code.

     public Transform[] targets;//were the AIs will go...
     public Sprite[] People;
     public SpriteRenderer mySprite;//the referance for the SpriteRenderer
     float posXAI;//X Position of object
     float posYAI;//Y position of object
     int index;//which is the next target
     IAstarAI agent;//get infomation on how to pathfind...
 
     void Awake()
     {
         posXAI = transform.position.x;//get X positon of sprite...
         posYAI = transform.position.y;//get Y psotion of sprite...
         mySprite.gameObject.GetComponent<SpriteRenderer>();
         agent = GetComponent<IAstarAI>();
         //Debug.Log(posXAI);
         //Debug.Log(posYAI);
     }
     void Update()
     {
         MoveAI();
         ChangeSpite();
     }
     void ChangeSpite()
     {
         if (posXAI < transform.position.x)
         {
             mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = null;
             mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[1];
             Debug.Log(People[1]);
         }
         if (posXAI > transform.position.x)
         {
             mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[2];
         }
         if (posYAI < transform.position.y)
         {
             mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[0];
         }
         if (posYAI > transform.position.y)
         {
             mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[0];
         }
     }

I thank anyone that comes along to help me. I've been working on this for a few days and I'm completely stumped.

Comment
Add comment · Show 2
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 AndrewBrown123 · Mar 20, 2020 at 02:16 PM 0
Share

So I have fixed SO$$anonymous$$E of the code only Left and Right and it not perfect. I still have to make it so Up and Down work.

Here it is now.

 if (transform.position.x < posXAI)//right
         {
             isRight = true;
             if (isRight)
                 isLeft = false;
                 //mySprite.sprite = null;
                 mySprite.flipX = false;
                 //mySprite.sprite = Person2;
                  Debug.Log("Sprtie2");
         }
         if (transform.position.x > posXAI)//left
         {
             isLeft = true;
             if (isLeft)
                 isRight = false;
                 mySprite.flipX = true;
                 //mySprite.sprite = null;
                 //mySprite.sprite = Person3;
                 //mySprite.GetComponent<SpriteRenderer>().sprite = Person3;
                 Debug.Log("Sprite3");
         }

I have change a bit of the code because of trial and error I now have 3 sprite ins$$anonymous$$d of an array and 4 private bool that tell the code if it's moving left or right (I probably don't need the bools but for now it's there).

avatar image AndrewBrown123 · Mar 26, 2020 at 01:22 PM 0
Share

Well I'm an idiot I thought I had to get the positions of all the Transforms I had as targets but that was not the case. What I needed to do was get the AI's velocity on the x axis. Now I just need to be able to get the sprite to change on y axis. When I get that I'll post it here for who ever else finds this.

Here is my new code

     void ChangeSpite()
     {
         if (agent.velocity.x <= -1)
         {
             Debug.Log(agent.velocity.x + "isLeft");
             mySprite.flipX = true;
         }
         if (agent.velocity.x >= 1)
         {
             Debug.Log(agent.velocity.x + "isRight");
             mySprite.flipX = false;
         }
     }

1 Reply

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

Answer by AndrewBrown123 · Mar 26, 2020 at 02:16 PM

OK so I finally got it. This took way too long but it at a point were I'm happy with it and it dose what I want to do. Look in the direction it's moving.

You need A* Pathfinding to fully use the script but this can be used for other types of 2D movement.

Here is the full script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Pathfinding;
 
 public class PedAI : MonoBehaviour {
     /*Too make this script work in any way you must...
     --> Place Seeker Script, and AIPath or Lerp or RichAI Script on object...
     --> DO NOT PUT THIS AS CHILED OF ANY GAMEOBJECT...
     --> THIS WILL MAKE A* NOT WORK...
     */
     public Transform[] targets;//were the AIs will go...
     public Sprite Person1;
     public Sprite Person2;
     public Sprite Person3;
     public SpriteRenderer mySprite;//the referance for the SpriteRenderer
     int index;//which is the next target
     IAstarAI agent;//get infomation on how to pathfind...
 
     void Awake()
     {
         mySprite.GetComponent<SpriteRenderer>().sprite = Person1;
         agent = GetComponent<IAstarAI>();
     }
     void Update()
     {
         MoveAI();
         ChangeSpite();
     }
     void ChangeSpite()
     {
         if (agent.velocity.x <= -1)
         {
             //Debug.Log(agent.velocity.x + "isLeft");
             mySprite.sprite = Person1;
             mySprite.flipX = true;
         }
         if (agent.velocity.x >= 1)
         {
             //Debug.Log(agent.velocity.x + "isRight");
             mySprite.sprite = Person1;
             mySprite.flipX = false;
         }
         //there is only one sprite for now... 
         //when I make another one it will look better... 
         if (agent.velocity.y <= -1)
         {
             Debug.Log(agent.velocity.y + " isDown");
             mySprite.sprite = Person3;
         }
         if (agent.velocity.y >= 1)
         {
             Debug.Log(agent.velocity.y + " isUp");
             mySprite.sprite = Person3;
         }
     }
     void MoveAI()
     {
         if (targets.Length == 0) return;
         bool search = false;
         // Check if the agent has reached the current target.
         // We must check for 'pathPending' because otherwise we might
         // detect that the agent has reached the *previous* target
         // because the new path has not been calculated yet.
         if (agent.reachedEndOfPath && !agent.pathPending)
         {
             //index = index + 1;
             index = Random.Range(0, targets.Length);
             search = true;
         }
         // Wrap around to the start of the targets array if we have reached the end of it
         index = index % targets.Length;
         agent.destination = targets[index].position;
         // Immediately calculate a path to the target.
         // Note that this needs to be done after setting the destination.
         if (search) agent.SearchPath();
     }
 }

  
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

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

Changing SpriteRenderer order in script 1 Answer

What is the best type of sprites for 2D top-down game 0 Answers

Getting a Material error from the default Sprite GameObject. (Unity 2017.3.0f3) 0 Answers

Why are my sprites wierd in Unity? 0 Answers

Unity Sprite slicing samples from adjacent cell 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