Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 MosquitoByte · Sep 12, 2019 at 04:04 PM · renderingscripting beginnereffects

Ghosting effect for sprites

I am making a ghosting effect for when sprites move. It works by having the parent object instantiate a prefab object with the same sprite as the parent object, rendered with some added opacity, and the ghost destroys itself after a bit. The problem is that i want to make this more flexible, as in i can apply this effect to any sprite, currently however, it only works because i have the parent object hardcoded as part of the ghost prefab's code. Is there some way i can instantiate the ghosts as children of whatever instantiated them, and have access to the parent, so that no matter what object i apply this to, it can become its ghost? examples of the code below:

-on the parent object:

 void Update()
     {     
         Instantiate(ghostfab)
      }

meanwhile, the script inside the ghost prefab which has been instantiated:

 public class GhostScript : MonoBehaviour
 {
     
     SpriteRenderer sprite;
     float timer = 0.12f;
     public GameObject WhatIGhost;
     // Start is called before the first frame update
     void Start()
     {
 
         WhatIGhost = GameObject.Find("ball");
 
         //get the renderer of this sprite
         sprite = GetComponent<SpriteRenderer>();
 
         //get the object to ghost's sprite
         sprite.sprite = WhatIGhost.GetComponent<SpriteRenderer>().sprite;
 
         //set the position to match the object to ghost
         transform.position = WhatIGhost.transform.position;
 
         //set the scale to match the object to ghost
         transform.localScale = WhatIGhost.transform.localScale;
 
         //set transparency
         sprite.color = new Vector4(50, 50, 50, 0.2f);
     }
 
     // Update is called once per frame
     void Update()
     {
         timer -= Time.deltaTime;
 
         if(timer <= 0)
         {
             Destroy(gameObject);
         }
     }
 }
Comment
Add comment · Show 1
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 MosquitoByte · Sep 15, 2019 at 08:21 PM 0
Share

i should also add for clarity that i'm using C#

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sisus_co · Sep 13, 2019 at 07:45 AM

You can instantiate the ghost script as a child by passing in the parent as a second parameter to the Instantiate method.

Also you should not use the Update method here which is called every single frame, but the Start method, so that only one instance of GhostScript is created.

 public GhostScript ghostPrefab;

 // Start is called before the first frame update
 void Start()
 {   
     // Instantiate the ghost prefab as a child of this transform
     Instantiate(ghostPrefab, transform);
 }


You can then acquire the parent inside the GhostScript using the transform.parent property.

 // Start is called before the first frame update
 void Start()
 {
     var WhatIGhost = transform.parent;
         
         ...


An alternative way to do this would be to manually call a method that sets up GhostScript, and passing the parent as a parameter, instead of using the Start method.

This way the code would not rely on the parent being assigned before the Start method is called, making it more reliable.

 public class Ball : MonoBehaviour
 {
     public GhostScript ghostPrefab;
 
     void Start()
     {   
         // Instantiate the ghost prefab as a child of this transform
         var ghost = Instantiate(ghostPrefab, transform);
         ghost.Setup(transform);
     }
 }
 
 public class GhostScript : MonoBehaviour
 {
     float timer = 0.12f;
 
     public void Setup(Transform WhatIGhost)
     {
         //get the renderer of this sprite
         var spriteRenderer = GetComponent<SpriteRenderer>();
 
         //get the object to ghost's sprite
         spriteRenderer.sprite = WhatIGhost.GetComponent<SpriteRenderer>().sprite;
 
         //set transparency
         spriteRenderer.color = new Vector4(50, 50, 50, 0.2f);
     }
     
     ...
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
avatar image
0

Answer by MosquitoByte · Sep 13, 2019 at 01:01 PM

attempting the first method gives me an error when setting WhatIGhost to transform.parent: "Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.GameObject'. As for the second, it seems to have mixed both scripts, let me explain to avoid confusion

-Ball with script that instantiates the ghost in update

-Ghostprefab which gets instantiated by the ball, is an empty gameobject with just a spriterenderer, and Ghostscript.cs

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.

Follow this Question

Answers Answers and Comments

137 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a sprite that change its texture when you go behind it? 0 Answers

Camera layering with post processing? 0 Answers

How to create effects that encase objects? 0 Answers

Baked Bloom/Glow (or one layer glow) 1 Answer

Rendering Effects 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