Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sprite moving with no input

Discussion in 'Scripting' started by ajlott, Feb 18, 2014.

  1. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    Hello,
    I've been working with Unity off and on for a while, but mostly just dabbling while I try out different engines to see which one I like best. Now I have a solid concept for a game I want to create and I've decided to use Unity for it. To start, I made a small scene to test out some of the mechanics I want to include in the game and I have this one problem that is vexing me to the point of exasperation. When I have my character sprite climbing a ladder, he continues moving in the same direction as when I released the arrow button, albeit at a much slower rate. Does anyone have any idea what might be causing this? The relevant code snippet is included below and I also want to point out that the Debug.Log() call reveals that moveUp is in fact 0 when no buttons are being pressed. Thanks.

    Code (csharp):
    1.  
    2.         moveUp = Input.GetAxis ("Vertical");
    3.         Debug.Log (moveUp);
    4.        
    5.         if (Physics.Raycast (transform.position, new Vector3(0, 0, 1), out hit, rayLength)) {
    6.             if (hit.collider.gameObject.tag == "ladder")
    7.                 climbing = true;
    8.             else
    9.                 climbing = false;
    10.         }
    11.  
    12.         if (climbing  Mathf.Abs (moveUp) > 0) {
    13.             rigidbody.useGravity = false;
    14.             rigidbody.velocity = new Vector3(0, moveUp * maxClimbSpeed, 0);
    15.         }
    16.         else if (!climbing)
    17.             rigidbody.useGravity = true;
    18.  
     
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    change this
    rigidbody.velocity = new Vector3(0, moveUp * maxClimbSpeed, 0);

    to this

    transform.Translate(Vector3.up * moveUp * maxClimbSpeed);

    See if its any better
     
  3. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    That makes it move ridiculously fast where before it moved at my intended speed. It did fix the drifting sprite, issue, so it's a starting point. I just need to dial in moveSpeed to get it where I want it. Thanks.

    EDIT: I also noticed that if I don't get my character sprite just right on the ground at the bottom of the ladder, he'll tunnel through the floor, hang out for a second and then start rising without responding to any of the controls I use. I believe this has something to do with an issue I read about recently where Box2D doesn't play well with the non-physics functions.
     
    Last edited: Feb 18, 2014