简体   繁体   中英

Make character follow mouse using Character Controller

I have a 2D space shooter game (imagine something like Chicken Invaders) that has been released on iOS and Android. I am now trying to port it to WebGL, and to do that, I'm trying to implement movement using the mouse. Having previously had success in implementing the movement using Unity's built-in character controller on the mobile platforms, I'd like to keep using it in the WebGL build to keep the code-base small and with as little platform-specific implementation as possible.

Therefore, what approach should I use to get the ship to follow the mouse cursor?

PS: On mobile I used touch.deltaPosition with characterController.Move() to have the ship follow the finger's movement.

EDIT: Current method

Input manager

public Vector2 GetInput () {
        switch(type) {
            case PlatformType.Computer:
                if (Input.GetButtonDown("Jump") || Input.GetAxis("Fire") != 0f) {
                    thisDelegate.InputDidReceiveFireCommand();
                }
                if (!Input.mousePresent) {
                    temp.x = Input.GetAxis("Horizontal");
                    temp.y = Input.GetAxis("Vertical");    
                } else {
                    if (Input.GetAxis("Mouse X") != 0f || Input.GetAxis("Mouse Y") != 0f) {
                        temp.x = Input.GetAxis("Mouse X");
                        temp.y = Input.GetAxis("Mouse Y");
                    } else {
                        temp.x = Input.GetAxis("Horizontal");
                        temp.y = Input.GetAxis("Vertical");        
                    }
                }
}

PlayerController

void HandleInput()
    {
        mvm = InputController.GetInput();
        if (GetComponent<InputManager>().type == InputManager.PlatformType.Mobile) {
            mvm *= Time.deltaTime * initialVeloc / 10;
        } else {
            if (mvm != Vector3.zero) {
                mvm -= transform.position;
            }
        }
    }

void Update () {
    characterController.Move((Vector2)mvm);
}

Current method makes the character jitter next to the center of the screen.

I've modified my script and I'm now taking the absolute mouse position instead of the delta. Here's how it looks now:

Input Manager:

            if (Input.GetAxis("Mouse X") != 0f || Input.GetAxis("Mouse Y") != 0f) {
                temp = Input.mousePosition;
                temp.z = 12.5f;
                //                Vector3 initialMov = mvm;
                temp = Camera.main.ScreenToWorldPoint(temp);
                temp -= transform.position;
            }

Controller

mvm = InputController.GetInput();
mvm *= Time.deltaTime * initialVeloc;
characterController.Move((Vector2)mvm);

This method works just as intended and seems more efficient than the RayCasting method, in terms of CPU usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM