0
\$\begingroup\$

I use Touch movement and if I jump and hit a corner with the feet of my player, my player rotates but I don't want this. Can anyone help me jumping without rotating if I hit anything ?

here is my Script:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed = 10, jumpVelocity =  10;
    public LayerMask playerMask;
    public bool canMoveInAir = true;
    Transform myTrans, tagGround;
    Rigidbody2D myBody;
    bool isGrounded = false;

    private Animator anim;

    // Use this for initialization
    void Start () 
    {
        myBody = this.GetComponent<Rigidbody2D>();
        myTrans = this.transform;   
        tagGround = GameObject.Find (this.name + "/tag_ground").transform;

        anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void FixedUpdate () 
    {
        isGrounded = Physics2D.Linecast (myTrans.position, tagGround.position, playerMask);

        #if UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
        Move(Input.GetAxisRaw("Horizontal"));   
        if(Input.GetButtonDown("Jump"))
           Jump();
        #endif
    }

    public void Move(float horizontalInput) 

    {
            //Er klebt nicht mehr an den Waenden
        if (!canMoveInAir && !isGrounded)
            return;

        Vector2 moveVel = myBody.velocity;
        moveVel.x = horizontalInput * speed;
        myBody.velocity = moveVel;  

        anim.SetFloat ("speed", Mathf.Abs (GetComponent<Rigidbody2D> ().velocity.x));

        if (horizontalInput < 0) 
        {
            transform.localScale = new Vector2 (25, 25);
        }
        if (horizontalInput > 0) 
        {
            transform.localScale = new Vector2 (-25, 25);
        }   
    }


    public void Jump()
    {
        if (isGrounded)
            myBody.velocity += jumpVelocity * Vector2.up;
    }
}
\$\endgroup\$
0
\$\begingroup\$

Disable the rotation on the physics object that's part of your character. There's a few ways of going about that. You can do it via scripts:

void Start () 
{
    myBody = this.GetComponent<Rigidbody2D>();
    myBody.freezeRotation = true;

Or you can freeze specific axis of rotation in the Unity editor using the Inspector (found under the constraints section):

enter image description here

\$\endgroup\$
  • \$\begingroup\$ You can mark the answer as correct if it helped you. \$\endgroup\$ – MichaelHouse Mar 25 '16 at 12:59
0
\$\begingroup\$

Alternatively to Byte56's answer, you can cancel the rotation, but not lock it. If you want to stop the rotation but not necessarily freeze it, you can use myBody.angularVelocity = Vector3.zero.

This means it will stop rotating, but if a new force is applied, it will start a fresh rotation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.