W
W
WalloutDay2015-11-01 13:25:22
Android
WalloutDay, 2015-11-01 13:25:22

Error in Character Controller Unity 2D code, what should I do?

I wrote a code to control the character on android, everything works. But here's a problem (
1-when you hold down the button (gui imagine) it works only as one click and in order for the character to go further, you need to click again. How to do or what to change in the script so that when the button is pressed, it goes until I 2- Character
animation.When the character is standing, the idle animation is played (because the "Speed" variable in the animator window is set to "0.0").As soon as he starts moving, the animation changes to walk (because the "Speed" variable in the window, the animator switches to "1.0"), BUT as soon as the button stops working, i.e. we release it (but for now it releases itself), the variable does not change. As it was, 1.0 remained, only sometimes after a couple of seconds returns to 0.0. Help what to do?
Here is the script.

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class CharacterController2 : MonoBehaviour {
  
  public float maxSpeed = 10f;
  bool facingRight = true;
  
  Animator anim;
  // Use this for initialization
  void Start () {
    anim = GetComponent<Animator> ();

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

  }

  public void Walk(int InputAxis)
  {
    float move = Input.GetAxisRaw ("Horizontal");

    move = InputAxis;
    
    anim.SetFloat ("Speed", Mathf.Abs (move));
    
    GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

    if (move > 0 && !facingRight) 
      Flip ();
    else if (move < 0 && facingRight)
      Flip ();

  }
  
  void Flip() {
    facingRight = !facingRight;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrDywar Pichugin, 2015-11-01
@WalloutDay

Unity Guide
1) You need to read the article Unity3D Best Practices
(don't request objects on every click/frame, cache them in class fields; you need to move an object with a rigidbody only in FixedUpdate, etc.)
1.1) To cache the elements of the current object, use Awaik() { ... }. To refer to other objects, use Start() { ... /* other objects will already be created, so you will definitely find them here */ }.
2) To make the character walk constantly while holding the button, make a flag (true/false). We pressed the button, called the PointerDown event through the Event Trigger (or any other event in your script, everything is up to you), set the flag to true in it, in FixedUpdate we have the condition if ( this._move ) { ... } the character went. The PointerUp button was pressed, the flag was set to false, the character stopped walking. The whole point is that the movement occurs only in FixedUpdate, you only need to switch the flag on or off from other methods / method. You can attach any script to the button and call a public method from it.
3) I did not understand the animation. For the on / off state, it is better to use bool, SetBool like, and this means that the value must be set in the code yourself, it will not return to false until you yourself wish it (a trigger is used for such purposes).
Take a free MVA course in 2D and 3D game development .
I myself regularly forget many things, I have to re-read them all the time. I rarely use.
If you seriously decide to get into game development and programming in general, then you need to learn English and another one of the popular ones for the development of thinking, https://www.busuu.com/enc/ for free.
90% of all issues have already been resolved, you just need to be able to search. I used to spend a lot of time on this, now even complex issues are resolved relatively quickly.

V
Vladimir Limarchenko, 2015-11-15
@vovo801

Input.GetAxisRaw ("Horizontal");
This should be done in Update or FixedUpdate.

public void Walk(int InputAxis)
  {
    float move = Input.GetAxisRaw ("Horizontal");

    move = InputAxis;

Here it turns out you got Axis value. and then overridden it with an argument from the InputAxis method. So your move variable will always be equal to InputAxis.
Also, it is not clear in the code example where you are calling the Walk() method.
Without actually calling this method, nothing will work.
I would try like this:
void FixedUpdate () {
           Walk(Input.GetAxis("Horizontal"));
  }

And you can safely delete the line
from the Walk method, anyway, you then assign the value of Move to InputAxis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question