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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Movement along Z-axis during horizontal input (Bug?)

Discussion in 'Scripting' started by Troas, Jun 30, 2014.

  1. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I don't know why this is happening, I got it following this tutorial:




    Tried creating a different scene and it still happens. Basically even when ONLY using horizontal (x-axis) input, for whatever reason the player object experiences a z-axis transform in position. I'm starting to think it's because of a physics material I applied to the floor which makes a "slide" effect.

    Even if that's the problem I have absolutely no clue how to fix it, and I have tried messing with all the settings and haven't really found anyone experiencing this particular problem.

    I guess the only fix I can really do is to completely take away the physics mat. Then maybe I could apply a script to all objects, that can move, and add acceleration to them to give a sort of "slick surface" feel. That however still will not create the same effect that I want when the player collides and bounces off of objects. (There probably is still a way to do the bounce off effect with scripting but it seems to me that will be much less optimal than having a physics mat do most the heavy lifting rather than me making code that is sure to be less the optimal).

    Any pointers in the right direction is appreciated, I am not afraid to sit for hours trying to figure out code but in this case I feel completely lost.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Need to see your script.
     
  3. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public float moveSpeed;
    7.     public GameObject deathParticles;
    8.  
    9.     private float maxSpeed = 5f;
    10.     private Vector3 input;
    11.  
    12.     private Vector3 spawn;
    13.  
    14.    
    15.     void Start () {
    16.         spawn = transform.position;
    17.  
    18.     }
    19.  
    20.  
    21.     void FixedUpdate () {
    22.  
    23.         // Movement
    24.         input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
    25.  
    26.         if (rigidbody.velocity.magnitude < maxSpeed){
    27.             rigidbody.AddRelativeForce (input * moveSpeed);
    28.         }
    29.  
    30.         if (transform.position.y < -4) {Die ();}
    31.     }
    32.  
    33.     // Collision with enemy
    34.     void OnCollisionEnter (Collision other){
    35.         if (other.transform.tag == "Enemy") {
    36.             Die ();
    37.         }
    38.     }
    39.  
    40.     // Finish level trigger
    41.     void OnTriggerEnter (Collider other){
    42.         if (other.transform.tag == "Goal"){
    43.             GameManager.CompleteLevel();
    44.         }
    45.     }
    46.  
    47.     // Particle system on death and spawn
    48.     void Die (){
    49.         Instantiate(deathParticles, transform.position, Quaternion.Euler (270,0,0));
    50.         //yield return new WaitForSeconds(4);
    51.         transform.position = spawn;
    52.     }
    53.  
    54. }
    55.  
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Maybe you forgot to lock movement/rotation on unwanted axes in your rigidbody
     
  5. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I can't exactly lock movement on the z-axis cause my character needs that axis in order to move around the stage.

    If there is a way to negate z-axis movement when inputting x-axis movement I wouldn't know of a way to do that but I guess I could look that up. Didn't think of it that way till you said that.
     
    Last edited: Jul 1, 2014
  6. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    333
    my guess is that you might have your camera in position that it looks like as if you are controlling z-axis. but instead it is the x-axis. did you check if horizontal controls input is effecting the z value inside moving object transform?
     
  7. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I checked this, and it is indeed the object transforming on the z-axis as the camera doesn't move at all.
     
  8. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    333
    try using "addforce" instead of relativeforce ??
     
  9. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    No dice, same result.

    When I go to the right it slightly goes up z-axis, when I go left it slightly goes down z-axis. This is starting to get fustrating lol
     
  10. Kale

    Kale

    Joined:
    Aug 25, 2011
    Posts:
    103
    Are you using a joystick? The joystick might be a bit missed up. Try taking out the Z input and see if anything happens. If it's that, then you'd just need to put in a thresh hold. (Doesn't seem likely though, since you never mentioned it moving when it's still.)

    Could also put in a debug of your input Vector, to see if there are any numbers that aren't supposed to be there.
     
  11. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Forget movement. Did you lock X and Y rotations? Or do you need your character to look down when you jump? Just lock those rotations you aren't planning to use.
     
  12. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Locked all rotations as I do not need them.

    Not using a joystick, only keyboard input for now.

    I'll check debug tomorrow.
     
  13. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Checked it, no unwanted values that I could see.

    Should I just report this as a bug?
     
  14. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Can anyone figure this out? It's really throwing me for a loop now, no one seems to have an idea of where this problem is coming from and I'm not sure if it can be called a "bug" since I haven't seen it reproduced nor would I have any idea of how to reproduce it.
     
  15. Kale

    Kale

    Joined:
    Aug 25, 2011
    Posts:
    103
    Can you build a web player version to look at?
     
  16. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I can, I'll do that tomorrow.
     
  17. AssignMeaning

    AssignMeaning

    Joined:
    Aug 31, 2014
    Posts:
    4
    Did you figure it out? I'm lost on the mysterious Z movement too. I am using that same tutorial. The floor seems to be adding to the Z movement, because before I even press Left or Right, the player will make Z movement when it makes contact with the floor. I tested this by starting the player above the floor and having it drop upon play. Notice the real-time position readings on the player.
     
  18. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Yea I tried uploading it to the web, it wont upload for me for whatever reason (I've been stuck studying like hell for MCAT + school too).

    I'll sit down this Sunday (I promise) and figure out a way to upload it. I still haven't figured out the issue.
     
  19. Chris196

    Chris196

    Joined:
    Apr 16, 2015
    Posts:
    6
    Did u fixed it ? I have the same problem. I found out that my Transform-values keep chaning. ( Position and Rotation goes like from 3 to 4 than back to 2.5 then to 5 or whatever ) . I would like to know why this appears
     
  20. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I haven't looked at it in some time. I was never able to get uploading working for me in that version of Unity for some reason. I've moved on to a different project but I still have the old files on that one.

    I can go back and see if it does the same thing. I have a few guesses at what it may be though though:

    1) The physics layer he adds does something weird
    2) The way he calculates movement by "adding force" does something weird and maybe it is better to just transform/translate the object.
     
  21. Chris196

    Chris196

    Joined:
    Apr 16, 2015
    Posts:
    6
    i fixed this problem by locking all rotations in the rigidbody and rotate the player over my script :)