How to Add temporary speed changes

I’m trying to make it so when I press the M key it plays an animation for my character and stops said charecter from moving but I’m having some trouble so far, If someone can help I would appreciate it, here is the code I use

using System.Collections.Generic;
using UnityEngine;

public class PlayerCtrl : MonoBehaviour
{
  public float moveSpeed = 5f;

  public Rigidbody2D rb;

  Vector2 movement;

  void Start()
      {

      
      }
    
    void Update()
    {
       movement.x = Input.GetAxisRaw("Horizontal");
      movement.y = Input.GetAxisRaw("Vertical");

    if (Input.GetKeyDown(KeyCode.M))
        {(GetComponent<Animator>().SetTrigger("Trigger1");) and (moveSpeed=5f;)}
    }

void FixedUpdate()
{

rb.MovePosition(rb.position + movement *  moveSpeed * Time.fixedDeltaTime);


}


}

I’m sorry to hear you are “having some trouble” but I have no idea what that means.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:

If you have no idea what your code is doing, FIX THAT FIRST!

Time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.