Minus sign reversed on Key A and D

Hi there. To get my Rocket to rotate right I had to put the minus sign on the A key, not on the D key. This is the opposite of what the tutorial does. My code works great. Why is it different from the tutorial? BTW, my Scene view has Positive X pointing right on the 3-axis compass, and Positive Y of course is pointing up. Oh, just for fun I put extra effort into building my Rocket. It’s four child GameObjects (one capsule, one cylinder, and two cubes) under a Rocket parent empty GameObject.

Rocket Screenshot

Hi,

Does the camera have a negative scale or is it rotated?

Here’s a screenshot of my scene/game/inspector view for the Main Camera.
Does this help pinpoint the reason why my A & D key minus setting is the reverse of the tutorial?

Unfortunately, your screenshot looks fine, so the camera is very likely not causing the issue.

Four things are coming to my mind:

  1. Maybe the scale of the rocket root game object is negative? Please check its Inspector.
  2. There could be an issue with your code. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?
  3. The “wrong” keys are assigned in the Input Manager. Please take a look at the first screenshot in the manual. “A” must be assigned to the “Alt Negative Button”. “D” must be assigned to the “Alt Positive Button”. Is that the case in your project settings?
  4. Maybe there is a bug in your version of Unity. Which one do you use?

Hi Nina.
I checked the Rocket parent and its 4 child gameobjects. None of them have a negative scale.
I checked the Project Changes repo for this lecture. Didn’t see any code changes. It looks like the original lecture code remains unchanged.
Checked the Input Manager. The Alt Negative Button is set to a. Alt Positive Button is set to d.
I’m using Unity 2020.3.26f1, an LTS version. I’ll upgrade to the latest 2020 LTS and see what happens.
Thanks much for your investigating this mystery, Nina.
Jim

You could also try to test your code in a new scene. Create a new scene with a cube. Assign the Rocket script to that cube. Click the Play button and try to make that cube fly and rotate. If the same behaviour happens even though you did not do anything but assigning the Rocket script to the cube, the problem is either in your code or in Unity itself.

If nothing works, keep your current solution with the minus sign and proceed with the course. Once the game is complete and built, test the build on your current device and another device to figure out if the issue might be caused by the hardware.

I created a new project. Made a cube rocket. I copied the code from the tutorial project into a Movement script in the new project. Attached script to cube rocket. And created Rigidbody for it of course.
When I hit Play the rocket cube behaves the exact same way as in the tutorial project. Mysterious!
Below is the complete code for my Movement script. Do you see anything peculiar about it?

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

public class Movement : MonoBehaviour
{
private Rigidbody _rigidbody;
[SerializeField] float _upThrust;
[SerializeField] private float rotationThrust;
// Start is called before the first frame update
void Start()
{
_rigidbody = GetComponent();

}

// Update is called once per frame
void Update()
{
    ProcessThrust();
    ProcessRotate();
}

void ProcessThrust()
{
    if (Input.GetKey(KeyCode.Space))
    {
        _rigidbody.AddRelativeForce(Vector3.up * _upThrust * Time.deltaTime);
    }
}
void ProcessRotate()
{    
    if (Input.GetKey(KeyCode.A))
    {
        ApplyRotation(-rotationThrust);
    }
   else if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(rotationThrust);
    }
}

private void ApplyRotation(float rotationThisFrame)
{
    _rigidbody.freezeRotation = true;  //freezing rotation so we can manually rotate
    transform.Rotate(-Vector3.forward * rotationThisFrame * Time.deltaTime);
    _rigidbody.freezeRotation = false;  //unfreezing rotation so physics system can take over
}

}

I’m wondering if the “double” minus sign might be causing the problem. See these two lines in your code:

ApplyRotation(-rotationThrust); // A key
transform.Rotate(-Vector3.forward * rotationThisFrame * Time.deltaTime);

Minus times minus is plus. Try to remove the minus sign from the Vector3.forward.

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

Privacy & Terms