[Unity] Rotating a 3D character in 2D space

Discussion in 'Game Development (Technical)' started by Craig Johnson, Mar 10, 2016.

  1. Craig Johnson

    Craig Johnson New Member

    Joined:
    Mar 10, 2016
    Messages:
    3
    Likes Received:
    1
    I been struggling with this issue for 5 days now.

    been searching everywhere for answers and still nothing concrete.

    All I want is for my 3d character(Which is a ball) to look left and right.

    But the rotation inputs get messed up somehow, making my character turn in weird unexpected ways, for instance I press left, it may rotate 180 on the y axis, then when i press right, it rotates 32 degress, then 90, then 70, then 35 and so on.

    below is my code:

    Code:
    void Start()
        {
            rb2d = gameObject.GetComponent<Rigidbody2D>();
        
            lookRight = transform.localRotation;
            lookLeft = lookRight * Quaternion.Euler(0, 180, 0);
        }
        void FixedUpdate()
        {
            grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
      
        
            float move = Input.GetAxis("Horizontal");
        
            rb2d.velocity = new Vector2(move * maxSpeed, rb2d.velocity.y);
            if (move > 0 && !facingRight)
            {
      
                facingRight = !facingRight;
                transform.localRotation = lookRight;
                Debug.Log("Transform: "+transform.localRotation.ToString());
            }
            else if (move < 0 && facingRight)
            {
            
                facingRight = !facingRight;
                transform.localRotation = lookLeft;
                Debug.Log("Transform: "+transform.localRotation.ToString());
            }
        }
    I really need some help here, because I feel this should not be so difficult to figure out.



    If you need more info let me know, thanks.
     
  2. Scoper

    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    90
    Likes Received:
    21
    I tried out your script, but could not find anything wrong with it. The object to which it is attached rotates left and right nicely. Maybe there is something else in your scene affecting your 3D character? Try disabling all other scripts and game objects. Then it should work. Then start re-enabling them a few at a time until it breaks and you find your bug.
     
    bantamcitygames likes this.
  3. Craig Johnson

    Craig Johnson New Member

    Joined:
    Mar 10, 2016
    Messages:
    3
    Likes Received:
    1
    Apparently the problem is because I'm using a 2D rigidbody and 2D colliders on a 3D object.

    Did you use a 3D object with 3D everything else?
     
  4. Scoper

    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    90
    Likes Received:
    21
    I used a RigidBody2D on a 3D object. It worked fine for me.
     
    bantamcitygames likes this.
  5. Craig Johnson

    Craig Johnson New Member

    Joined:
    Mar 10, 2016
    Messages:
    3
    Likes Received:
    1
    Well I did it, changed the rigidbody and colliders to 3D and it works now :)
     
    bantamcitygames likes this.

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer