1
votes

I have a 2d game, where GameObject (GO) moves to the right side on the floor. I want to make it jump.

But when I add Character Controller to my GO and start simulation, it just fast move on the left side. Can't understand, what's wrong.

GO also contain Rigidbody and Box Collider

    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    private Vector3 moveDirection = Vector3.zero;

    void Start ()
    {
        player = (GameObject)this.gameObject;
    }

    void Update ()
    {
        // Move
        player.transform.position +=
            player.transform.right * speed * Time.deltaTime;

        // Jump    
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,
                                        Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;

            if(Input.GetKey(KeyCode.Space))
                moveDirection.y = jumpSpeed;
        }

        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move (moveDirection * Time.deltaTime);
    }
2

2 Answers

2
votes

This is old but came across this on a google search.

Character Controller is not designed to use the physics components. Remove the rigidbody and box collider. It uses a simulated capsule collider to do its detection.

Also remove the following from you code and the input should work like normal again.

player.transform.position += player.transform.right * speed * time.deltaTime;    

That piece of code is causing you to move inadvertently.

1
votes

You should first create PlayerController class with this code:

public class PlayerController : MonoBehaviour {

    public Vector2 moving = new Vector2();

    // Use this for initialization
    void Start () {

    }

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

        moving.x = moving.y = 0;

        if (Input.GetKey ("right")) {
            moving.x = 1;
        } else if (Input.GetKey ("left")) {
            moving.x = -1;
        }

        if (Input.GetKey ("up")) {
            moving.y = 1;
        } else if (Input.GetKey ("down")) {
            moving.y = -1;
        }

    }
}

Then create Player class with this code:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float speed = 10f;
    public Vector2 maxVelocity = new Vector2(3, 5);
    public bool standing;
    public float jetSpeed = 15f;
    public float airSpeedMultiplier = .3f;
    public float jump=2f ;


    private PlayerController controller;

    void Start(){
        controller = GetComponent<PlayerController> ();

    }

    // Update is called once per frame
    void Update () {
        var forceX = 0f;
        var forceY = 0f;

        var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
        var absVelY = Mathf.Abs (rigidbody2D.velocity.y);

        if (absVelY < .2f) {
            standing = true;
        } else {
            standing = false;
        }

        if (controller.moving.x != 0) {
            if(absVelX < maxVelocity.x){

                forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeedMultiplier);

                transform.localScale = new Vector3(forceX > 0 ? 1 : -1, 1, 1);
            }
        }

        if (controller.moving.y > 0) {
            if(absVelY < maxVelocity.y)
                forceY = jetSpeed * controller.moving.y;
        }

        rigidbody2D.AddForce (new Vector2 (forceX, forceY));
    if (Input.GetKey ("a")) {

            if (transform.localPosition.y < 1   ) 
                rigidbody2D.AddForce (new Vector2 (0, jump ));


                }

    }
}

You can fly with arrow keys and jump with "a" key. You can simply change that.