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 Dimitris4 · Apr 12, 2019 at 04:51 PM · collisioneffectslow

Problem with scripts

public class SlowGround : MonoBehaviour {

 private PlayerController SlowingSpeed;
 
 private void Start()
 {
     SlowingSpeed = GameObject.Find("Player").GetComponent<PlayerController>();
     
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         SlowingSpeed.speed -= 4;
     }
 }
 private void OnCollisionExit2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         SlowingSpeed.speed += 4;
     }
 }

}

I tried to make a ground which slows the player but I couldnt and I dont see the mistake here anywhere. all kinds of help would be appreciated. I dont know if it actually enters the other script and takes the speed variable but I think it does. here is the playerController script too. (too simple just move left and right)

public class PlayerController : MonoBehaviour {

public float speed;

 public float jumpForce;

 public Transform GroundCheck;

 public float RadiusCheck;

 bool isGrounded;

 Rigidbody2D rb2d;

 float PlayerInput;
 
 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }


 void FixedUpdate()
 {
     isGrounded = Physics2D.OverlapCircle(GroundCheck.position, RadiusCheck);
     PlayerInput = Input.GetAxis("Horizontal");
     rb2d.velocity = new Vector2(PlayerInput * speed, rb2d.velocity.y);
     Debug.Log(PlayerInput);
 }

}

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 metalted · Apr 12, 2019 at 08:55 PM 0
Share

Just as a tip, try to stick with one na$$anonymous$$g convention. It's fine to use PascalCase or camelCase, but don't mix them together. That will make the code more readable. What i personally use is camelCase for variables and PascalCase for functions. Also using names that make sense for the code is a good idea. Like for instance the player controller. It would make more sense to call it: "private PlayerController playerController" ins$$anonymous$$d of "private PlayerController SlowingSpeed". The variablename playerController tells you what it is pointing at, while SlowingSpeed does not and could mean multiple things. As i said this is just a tip, the most important thing is that it makes sense to you.

1 Reply

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

Answer by metalted · Apr 12, 2019 at 09:14 PM

I just made some changes to your script. Changed some names to make it more readable for example. Also, i added a function for changing the value. Setting the variable directly using .speed is fine, but as you said, you don't know if the value is even changed. Using a function helps you with that. Every time you change the value now, it will Debug a message. It will also give a message if the player isn't found. I can't see the problem with your code right away, but using Debug.Log will get you a long way to finding the problem.


 public class SlowGround : MonoBehaviour
 {
     private PlayerController playerController;
 
     private void Start()
     {
         //Get the PlayerController
         playerController = GameObject.Find("Player").GetComponent<PlayerController>();
 
         if(playerController == null)
         {
             Debug.Log("Player wasn't found!");
         }
 
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         //When collision has player tag
         if (collision.gameObject.CompareTag("Player"))
         {
             //Decrease player speed
             playerController.ChangeSpeed(-4f);
         }
     }
 
     private void OnCollisionExit2D(Collision2D collision)
     {
         //When collision has player tag
         if (collision.gameObject.CompareTag("Player"))
         {
             //Increase player speed
             playerController.ChangeSpeed(4f);
         }
     }
 }
 
 public class PlayerController : MonoBehaviour
 {
     public float speed;
     public float jumpForce;
     public Transform ground;
     public float circleRadius;
     public bool isGrounded;
     public Rigidbody2D rb2d;
 
     public void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     public void FixedUpdate()
     {
         float playerInput = Input.GetAxis("Horizontal");
         isGrounded = Physics2D.OverlapCircle(ground.position, circleRadius);
         rb2d.velocity = new Vector2(playerInput * speed, rb2d.velocity.y);
     }
 
     public void ChangeSpeed(float change)
     {
         speed += change;
 
         Debug.Log("Changing the speed value by: " + change);
     }
 
 
 }





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 Dimitris4 · Apr 13, 2019 at 06:14 PM 0
Share

Thanks for your time :). Although it didnt help I appreciated it. I copy-pasted your code and nothing changed. Neither in my console. Also, I checked the tags and I think they are all good.

avatar image Dimitris4 · Apr 13, 2019 at 06:20 PM 0
Share

Never$$anonymous$$ind, I removed the tags needed for the effect to happen and It worked just fine :)). When I saw the "Changing the speed value by -4" popping on my console Im was so thriled.

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

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

Trigger particles when collide 0 Answers

Translate and Yield giving inaccurate results? 2 Answers

Locking particle axis? 3 Answers

Wht isn't the sphere moving and rebounding properly? 1 Answer

Is this possible to add effect in exact location where two different objects are colliding? 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