1

I have a game object called "Player" and my map is made up of short grasses and long grasses. If my player is on the long grass, I want it to slow down. One additional problem is that there are multiple Long grasses game objects in my game. Here is a screenshot of them :

https://i.stack.imgur.com/kSxvf.jpg

This is what my current movement code looks like :

public class PlayerMovement : MonoBehaviour
{

    public Sprite Up;
    public Sprite Down;
    public Sprite Right;
    public Sprite Left;
    public float speed;
    private SpriteRenderer sr;


    // Update is called once per frame
    void Update()
    {

        Vector3 move;

        if (Input.GetKey(KeyCode.W))
        {
            GetComponent<SpriteRenderer>().sprite = Up;
            move = new Vector2(0, speed * Time.deltaTime);

            transform.position += move;
        }
        if (Input.GetKey(KeyCode.A))
        {
            GetComponent<SpriteRenderer>().sprite = Left;
            move = new Vector2(speed * Time.deltaTime, 0);

            transform.position -= move;
        }
        if (Input.GetKey(KeyCode.D))
        {
            GetComponent<SpriteRenderer>().sprite = Right;
            move = new Vector2(speed * Time.deltaTime, 0);

            transform.position += move;
        }
        if (Input.GetKey(KeyCode.S))
        {
            GetComponent<SpriteRenderer>().sprite = Down;
            move = new Vector2(0,speed * Time.deltaTime);

            transform.position -= move;
        }
    }
}

// All my code does is that on each WASD, it changes to a different sprite and moves it.

An explanation would be appreciated because I'm a beginner to this.

HSN720
  • 71
  • 2
  • 12

2 Answers2

3
  1. use colliders, eg. boxcolliders to detect if player is touching the grass.
  2. Use a tag on you player
  3. use the oncollisionenter2d() method on the grass
 void OnCollisionEnter2D(Collision2D col)  {
     if(col.tag == "PlayerTag") {
         playerScriptWhereSpeedIsLocated.speed = 1 //the speed you want
     } }
Stanley
  • 2,434
  • 18
  • 28
  • @HSN720 Seems like i answered your question, the error you are now getting is another problem, you should read up about it here: https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop If you do get the code to work, would you mind marking the question answered? – Stanley Oct 02 '19 at 18:18
1

One solution to this would be to use 2D Trigger Colliders.

Assuming your player has some sort of 2D collider attached to it, you may attach trigger colliders to your grass objects which can then send a message to the PlayerMovement script when the player enters/leaves the trigger by adding the OnTriggerEnter2D and OnTriggerExit2D methods to your PlayerMovement class. Here's an example of how this might work:

void OnTriggerEnter2D(Collider2D col)
{
    // You can use gameObject.tag to determine what type of object we're colliding with
    if(col.gameObject.tag == "LongGrass"){
        speed = .8f;
    }
}

void OnTriggerExit2D(Collider2D col)
{
    if(col.gameObject.tag == "LongGrass"){
        speed = 1f;
    }
}

I would encourage you to look into the Collision action matrix if you are having trouble detecting collisions in this way. You'll also make sure you tag your grass correctly for this method to work.