I’ve been bashing my head against this problem for hours and hours over many days and I’m convinced it must be something silly I’m not realizing. I’ve asked on a couple different web forums, but figured maybe the Unity forums themselves might be the best place to finally figure this out.
First of all, I know the controller is set up to work correctly with Unity because when I grabbed the code linked from the description in this guy’s video:
https://www.youtube.com/watch?v=mwrfV-2lpg8
Everything works correctly when I hit “play” in Unity. I can use my Xbox One controller to move the boxes around and the buttons change colors.
Great! But let’s go over to my code: https://github.com/djotaku/laserdefender
Specifically: https://github.com/djotaku/laserdefender/blob/master/Assets/Scripts/Player.cs
At the time that I write this, my Move() method looks like:
private void Move()
{
var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; //var is allowed when it's unambiguious what type it is (makes it somewhat Pythonic) multiplying by Time.deltaTime makes it framerate independent
var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
transform.position = new Vector2(newXPos, newYPos);
//transform.Translate(deltaX, deltaY, 0); // I think translate is needed for controllers to work, based on video and Unity documentation, but still not working for me
}
Right now it works with the keyboard, but the Xbox One controller doesn’t do anything. I’ve tried alternating between using transform.position and using transform.Translate. (Currently commented out since I’m following a class and so I want to stick with the professor’s code while I’m going through it) Neither works for the controller. I did a comparison of my Input preferences dialog box and the one in the Youtube video that has working code and it seems (at least for the relevant entries of Horizontal and Vertical) to look like the project I found on the Youtube video code.
The only thing that’s MAYBE a hint at what might be wrong is that in Visual Studio for me:
using System.Collections and using System.Collections.Generic
are greyed out (usually meaning not being used, right?) but in his code they are not greyed out. Don’t know if that’s a red herring.
PLEASE, this is driving me crazy - if you know what might be wrong in my code or in Unity - please let me know.