• 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 tonyjoseph456 · Jan 20, 2015 at 09:45 AM · animationgameobjectspritesspriterenderer

Placing the sprite game object in the players position and also enable and disable it when needed

I have a sprite game object in my unity game. It is an animation which is used to show when a collectible is collected by the player. So I need the animation to be played only when the player collects a collectibLe. also the sprite game object has to be positioned in the same position as that of the player. How does I do that? Any help from any one is easily appreciated. Thanks in advance.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fddefdef · Jan 20, 2015 at 10:28 AM

If i understood correctly your question you just have to write a script that instantiates that sprite you want at the player's position.

I assume that somewhere in your code you have called a collision function to detect when the player has collected the collectible object. Inside that function you could call the Instantiate function and set the position of the sprite at the player's position. To be clear you'll have something like that.

 OnCollisionEnter2D(collision2D collision)
 {
  if(collision.transform.tag == "collectible")
  {
    your code here
    Instantiate(sprite animation, transform.position, Quaternion.identity);
  }
 }

For the animation to be played only when the sprite appears, just uncheck the loop parameter of the animation from the inspector.

Comment
Add comment · Show 2 · 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
avatar image tonyjoseph456 · Jan 20, 2015 at 10:38 AM 0
Share

I had written the code for the collision detection and all. I only need to know, how to place the sprite renderer to the players position, since players position is world point and sprite renderer is in 2D. So that when coin collected the sprite animation plays. By the way my game is a 3D game.

avatar image shanky_91 · Jan 20, 2015 at 10:58 AM 0
Share

Sprite renderer is just a component attached to an object that exists in 3D world space and not in screen space. You just need to set the position of the sprite game object to that of the player.

avatar image
0

Answer by shanky_91 · Jan 20, 2015 at 11:44 AM

On a monobehaviour attached to the player use this method:

 void OnCollisionEnter(Collision collision) {
             
     if (collision.gameObject.tag == "collectable")
     {
        //Then create sprite
        //Set sprite transform.position to transform.position
     }
 }

you will also need to add a tag to the collectable called "collectable"

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Would changing the sprite need a new set of animation? 0 Answers

how can i get the clicked sprite which is placed on UI canvas? 1 Answer

how can i access multiple sprites using a single variable? 1 Answer

how to check presence of sprite on a UI button? 1 Answer

Alpha masking multiple 2D Sprite GameObjects with 1 Sprite GameObject 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges