- Home /
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);
}
}
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.
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);
}
}
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.
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.