Rotate Ship With Position & Throw Challenge (New InputSystem )


Here you can see few things.
1 . I Forgot to disable mouse pointer in ShareX
2. My values is not very good.
3. Roll based on input does not work well with keyboard. Value can only be 1 or 0.
4. Unity new InputSystem in action.

Reimplementation of lecture script in new system.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    [SerializeField] private InputAction fire = new InputAction("Fire", InputActionType.Button, null, null, null, "ButtonControl");
    [SerializeField] private InputAction move = new InputAction("Move", InputActionType.Value, null, null, null, "Vector2Control");
    [SerializeField] private float controlSpeed = 20f;
    [SerializeField] private float xRange = 20f;
    [SerializeField] private float yRange = 13f;
    [SerializeField] private float positionPitchFactor = -2.5f;
    [SerializeField] private float positionYawFactor = -0.5f;
    [SerializeField] private float controlPitchFactor = -1.5f;
    [SerializeField] private float controlRollFactor = -3f;

    private Vector2 movementDirection;

    private bool isFiring;
    private bool isMoving;

    private void OnEnable()
    {
        fire.Enable();
        move.Enable();
    }

    private void OnDisable()
    {
        fire.Disable();
        move.Disable();
    }

    private void Awake()
    {
        move.AddBinding("<Gamepad>/leftStick");
        move.AddCompositeBinding("2DVector")
            .With("Up", "<Keyboard>/w")
            .With("Down", "<Keyboard>/s")
            .With("Left", "<Keyboard>/a")
            .With("Right", "<Keyboard>/d");
        fire.AddBinding("<Mouse>/leftButton");
        fire.AddBinding("<Gamepad>/rightTrigger");
        fire.performed += ctx => OnFire();
        fire.canceled += crx => OnFire();
        move.started += ctx => OnMoveStarted();
        move.performed += ctx => OnMove(ctx);
        move.canceled += ctx => OnMoveCancel();
    }

    void Update()
    {
        ProcessInputState();
    }

    private void ProcessInputState()
    {
        if (isMoving)
        {
            Vector2 movementVector = movementDirection * (controlSpeed * Time.deltaTime);
            float NewX = Mathf.Clamp(transform.localPosition.x + movementVector.x, -xRange, xRange);
            float NewY = Mathf.Clamp(transform.localPosition.y + movementVector.y, -yRange, yRange);
            transform.localPosition = new Vector3(NewX, NewY, transform.localPosition.z);

            float pitch = transform.localPosition.y * positionPitchFactor + movementDirection.y * controlPitchFactor;
            float yaw = transform.localPosition.x * positionYawFactor;
            float roll = movementDirection.x * controlRollFactor;
            transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
        }
    }

    void OnFire()
    {
        isFiring = !isFiring;
    }

    void OnMoveStarted()
    {
        isMoving = true;
    }

    void OnMove(InputAction.CallbackContext ctx)
    {
        movementDirection = ctx.ReadValue<Vector2>();
    }

    void OnMoveCancel()
    {
        movementDirection = Vector2.zero;
        isMoving = false;
    }
}

New system premade components leaks memory. Wrapper generated from asset allows only one function per action and does not regenerated on asset save.

3 Likes

Very good job with your code :clap:

Thanks ! very useful.
One very annoying thing is that the keyboard input get straight values from 1 to 0 even with CompositeBindings on the left joystick, causing very snappy rotations.

I’m trying to get some sort of smooth damp on the vector 2 as showed here or here or here but still haven’t figured out how to apply it to our case.

I’ve been trying to figure this out too. Most of the answers seem to be that you have to do it yourself in code.

I think the solution here works:
https://answers.unity.com/questions/1700038/how-to-control-aixs-gravity-sensitivity-in-new-inp.html

The section of code posted by [Wollbobaggins] with the ‘inputGravity’ variable.
InputGravity numbers between 2 and about 4 appear to produce a ‘reasonable’ affect.

I’m going to try it more tomorrow.

From the Web:

My changes:
image

EDIT:
My X and Y variables were all messed up in this. The code worked, but was very confusing.
I posted a fixed version in the next comment.
Sorry for any confusion.

FYI: My X and Y values are reversed in this code. Looks like it got broken as I was trying to fix this issue.

Here’s the fixed version:

Privacy & Terms