Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Littlestrings · Sep 20, 2021 at 02:04 AM · 2d sprites

Changing 2D Player Sprite on Trigger using C#

Hi dear community,

i am still new to C# in unity. I am grateful for a solution to this little problem i have.

My objective is to change the Player's Sprite once they have collected a game item.

Please see my script below which does nothing to the player when she collides into her helmet.

What am I missing?

Thank you so much, Simone

using System.Collections; using System.Collections.Generic; using UnityEngine;

/// /// Makes the Player Walk Left & Right, Jump using animations /// public class GigiPlayerCtrl : MonoBehaviour { Rigidbody2D rb; public int speedBoost; public float jumpSpeed; // set this to 600 SpriteRenderer sr; Animator anim; bool isJumping; public Sprite gigiHelmet;

 // Start is called before the first frame update
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     sr = GetComponent<SpriteRenderer>();
     anim = GetComponent<Animator>();

 }

 // Update is called once per frame
 void Update()
 {
     float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be negative 1, -1 or 0.
     playerSpeed *= speedBoost; // playerSpeed = playerSpeed * speedBoost
     if (playerSpeed != 0)
         MoveHorizontal(playerSpeed);
     else
         StopMoving();

     if (Input.GetButtonDown("Jump"))
         Jump();

     


 }
 void MoveHorizontal(float playerSpeed)
 {
     rb.velocity = new Vector2(playerSpeed, rb.velocity.y);

     if (playerSpeed < 0)
         sr.flipX = true;
     else if (playerSpeed > 0)
         sr.flipX = false;
     if (!isJumping)
         anim.SetInteger("State", 1);

 }

 // Update is called once per frame
 void StopMoving()
 {
     rb.velocity = new Vector2(0, rb.velocity.y);
     if (!isJumping)
         anim.SetInteger("State", 0);
 }
 void Jump()
 {
     isJumping = true;
     rb.AddForce(new Vector2(0, jumpSpeed)); // Make the player jump upwards ( the Y axis)
     anim.SetInteger("State", 2);
     AudioManager.instance.PlaySFX(1);
 }

 private void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.CompareTag("Ground"))
     {
         isJumping = false;
     }

     else if (other.gameObject.CompareTag("Helmet"))
     {
         gameObject.GetComponent<SpriteRenderer>().sprite = gigiHelmet;

     }

 }

   

}

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 VisionElf · Sep 20, 2021 at 01:59 PM

  • Have you checked that your OnCollisionEnter2D is correctly called?

  • Did attached the tag "Helmet" to your gameObject you're trying to collide with?

  • Have you assigned the gigiHelmet to a non-null value?

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 Littlestrings · Sep 20, 2021 at 09:32 PM 0
Share

@VisionElf Have you checked that your OnCollisionEnter2D is correctly called? I am not to sure how to check that..

Did attached the tag "Helmet" to your gameObject you're trying to collide with? Yep!

Have you assigned the gigiHelmet to a non-null value? I do not know how to do that.

Will the animations be impacted, i tried to do this via animator but could not get it to work .

Thank you for your time.

avatar image VisionElf Littlestrings · Sep 21, 2021 at 10:37 AM 0
Share

You can use Debug.Log in your OnCollisionEnter2D method. If you see the message in the console, it means it's correctly called.

avatar image
0

Answer by Littlestrings · Sep 21, 2021 at 09:27 PM

@VisionElf thank you, i will try that.

How do I assign the gigiHelmet to a non-null value? What does that do?

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

129 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

Related Questions

2D Z-Levels HELP Needed! Can I script multiple objects to run on z levels,,2d Multiple Z levels in one scene 0 Answers

Sprite sheet editor not working 0 Answers

Making a 2D Ragdoll 2 Answers

Large Image/Sprite Renderer components cause FPS drop on device 0 Answers

How to make parts of a 2D ragdoll not collide with eachother? 2 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