My ship only moves to the left (negative x axis)

The moment I hit play, my ship immediately starts moving along the negative X axis. Key inputs are ignored? Where do I look to find the problem?

Hi,

Have you already clamped the position values? If so, the first thing I’d do is to check the clamp values with Debug.Log during runtime. Also move the player ship in the scene to figure out what the actual coordinates of the edges of your game screen are. Maybe the values in your code are completely wrong even though they make sense “on paper”.

Can you provide your code?

I don’t believe I have anything clamped. I’m not sure HOW to clamp something…

Here’s the code

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

public class PlayerControls : MonoBehaviour
{
    [SerializeField] float controlSpeed = 10f;

    // Update is called once per frame
    void Update()
    {
        float xFly = Input.GetAxis("Horizontal");
        float yFly = Input.GetAxis("Vertical");

        float xOffset = xFly * Time.deltaTime * controlSpeed;
        float newXPos = transform.localPosition.x + xOffset;

        transform.localPosition = new Vector3 (newXPos, transform.localPosition.y, transform.localPosition.z);

    }
}

I copy-pasted your code and found nothing, it works fine, so not sure what might be causing this.

Based on a rapid look at the course’s content (I took the previous one, so not sure what has changed), the only reason I can think of that might be causing this, is that there’s some sort of conflict with the timeline, something not code related, but I cannot tell for certain.

Yeah, I’m redoing the module from scratch to see…

Figured it out. As I suspected, it was a dumb issue on my part.

Because the Input Manager defaults to adding Joystick motion, it was reading inputs from the throttle quadrant I use for my flight sim as a joystick. If I unplug the throttle device, or rename/deactivate the Joystick input to ‘HorizontalJoystick’, so that the script doesn’t read that input, it all works as planned.

Silly me.

Good job on solving the problem, and thanks a lot for sharing your solution here. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms