What's wrong with my argon assault code

Hello,
For some reason my plane in arcana express won’t turn using the roll factor. When I tried to turn the roll factor to the highest it just would glitch. Here is my code. Can anyone see what I did wrong?

Please contact me if you need more information.

Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you tried to use lower values in the Inspector?

Hope this helps :slight_smile:


See also;

1 Like

I have compared my code to the Lecture Project Changes repeatedly. I’ve noticed no notable differences. Here is my full code:

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

public class playerController : MonoBehaviour
{
[SerializeField] float xSpeed = 5f;
[SerializeField] float ySpeed = 5f;
[SerializeField] float xRange = 5f;
[SerializeField] float yRange = 3f;

[SerializeField] float positionPitchFactor = -2f;
[SerializeField] float controlPitchFactor = -10f;
[SerializeField] float positionYawFactor = 2f;
[SerializeField] float controlRollFactor = -20f;


// [SerializeField] float yRangeMin = 4f;
// [SerializeField] float yRangeMax = 6f;

float xThrow, yThrow;
// Update is called once per frame
void Update()
{
    ProcessTranslation();
    ProcessRotation();
    ProcessFiring();
}

void ProcessRotation()
{
    float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
    float pitchDueToControl = yThrow * controlPitchFactor;
    
    float pitch = (pitchDueToControl + pitchDueToControl);  // * Time.deltaTime;
    float yaw = (transform.localPosition.x * positionYawFactor); // * Time.deltaTime;
    float roll = (xThrow * controlRollFactor); // * Time.deltaTime;
    
    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
void ProcessTranslation()
{
    xThrow = Input.GetAxis("Horizontal") * Time.deltaTime;
    yThrow = Input.GetAxis("Vertical") * Time.deltaTime;

    float xOffset = xThrow * xSpeed;
    float xRawPos = xOffset + transform.localPosition.x;

    float yOffset = yThrow * ySpeed;
    float yRawPos = yOffset + transform.localPosition.y;

    float clampedXPos = Mathf.Clamp(xRawPos, -xRange, xRange);
    float clampedYPos = Mathf.Clamp(yRawPos, -yRange, yRange);

    // Debug.Log("Turning left or right: " + horinzontalThrow);
    // Debug.Log("Turning up or down: " + verticalThrow);

    transform.localPosition = new Vector3(clampedXPos, clampedYPos,
    transform.localPosition.z);
}
void ProcessFiring()
{
    if (Input.GetButton("Fire1"))
    {
        Debug.Log("Firing Laser Bullets.");
    }
    else if (Input.GetButton("Fire2"))
    {
        Debug.Log("Firing Shotgun Shells.");
    }
    else
    {
        Debug.Log("Not firing.");
    }
}

}

float xThrow, yThrow;

It’s been forever since I’ve done this one, but I’d bet the xThrow and yThrow not being set would be what’s causing you issues.

change to:
public float xThrow, yThrow;

and set their value to some positive number and I’d bet it’ll start working… or at least moving.

EDIT: Scrap that. I saw where it’s done. That won’t work. Maybe your xRange and yRange need to be different numbers due to placements of assets? I don’t think I’m thinking straight. I need sleep. Night.

Thank you for sharing your code as text. Have you already animated the Player Rig? If so, disable the timeline for a moment and test your game again. If the playerController script works, the problem is in the timeline animation. If it still does not work, check your console. Maybe there is an error message. Also make sure that this script is attached to the correct game object.

I apologize for the late reply. I’ve disabled the timeline.and the problem still persists. I deleted the script from the playerShip and then reapplied it to no avail. In the console their is no error message. And the script is attached to the playerShip itself.

Welcome back. :slight_smile:

Just to ensure that I understood you correctly: You disabled the timeline but the player ship still does not roll as expected?

If so, comment out the ProcessRotation method call in the Update method, save your script and test your game again. Given you identified the problem correctly*, the ship is not supposed to glitch anymore. If it still glitches, the problem is very likely somewhere else.

In that case, check the xRange and yRange values. Move the ship manually to the edges of your screen and check the position values in the Inspector to learn what clamp values should be working. If xRange and yRange are not within the range you figured out, that could explain the glitching.


* To avoid any misunderstandings: When encountering problems in programming, we make an assumption and try to verify/falsify that assumption to either solve the problem or to narrow it down further. What we see/think first is often just a part of the problem or a side-effect, not the actual problem. However, since we are no clairvoyants, we have to play “private investigator”: Gather information, try to interpret it (assumption), try to verify our interpretation (proof).

When using the term “assumption” in this context, I’m referring to “the way of debugging problems” only. It does not have any negative connotation in the context of programming and does not refer to anybody’s skills or intelligence.

I just want to make this clear because there had been a misunderstanding a couple of days. :slight_smile:

Regarding the “assumption” that’s quite all right. I’ve had friends email me about that regarding my 3d models and use similar wording. After disabling processRotation I’ve experienced no glitches. I’ve switched to my new computer which runs much smoother, so I thought the problem would be solved, but the problem still occurs to a lesser extent.

Since the game window is just a laggy preview, did you try to build your game and test the build?

And did you enable ProcessRotation on your new computer or did the problem persist without having ProcessRotation enabled again?

I enabled processRotation on my new computer, and the problem persisted. After I disabled processRotation the problem still exists. I’ll try to build the game when I’m more available

If the issue persists, could you please record a video or explain the issue again in other words? Maybe I’m just misunderstanding your problem.

You were partially correct. When I built the code the problem stopped, the glitch disappeared. I just got a new pc and the specs are leagues better than my old one. Here are the recordings of both my built version and my unity editor version. Since the problem stopped in build mode (except for that one time during the recording.) I think I can move on with the program since I know this won’t affect the final product.

I’ve sent the video through email since I can’t send video files on this platform

You could upload the video to Youtube and share a link here.

It’s alright. The problem is fixed. When I build the final result there should be no glitches. If there are any problems, I’ll be sure to tell you. Thank you for your help.

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

Privacy & Terms