My ship stopped moving

I animated all my enemy paths and then closed Unity. After a while, I came back and when I played the game, the ship wouldn’t change its on-screen location. The camera animation was working, and the ship was tilting left, right, up and down. But it was just fixed in one point. The closest it came to moving was small jitters.

This happened to me once before but it was fine after I deleted the animation on the enemies. I can’t really do that now though because I have animated a good minute and half of the various enemies.

Hi Ishaan,

Are there any error messages in your Console? If not, check which game object you animated. If you animated the same object which also gets moved via code, there might be a conflict, and the animation might override the values which were calculated by your code.

No messages are appearing in my console. And the objects that are animated are the enemy ships which are children of empty game objects. Everything was working fine until I reopened Unity. Ill attach a video clip so you know what I am talking about. (Using a mega link since I can’t upload videos here. You might need to download it, sorry about that)

https://mega.nz/file/phkQgSIJ#HOyVpDiJl2J_gA1HA7nW1afMRwB43UOh8AdDn3q346I

Thank you. The video was helpful. :slight_smile:

The best would be to log all your relevant values into your console, for example, the ones for the clamping. You could also try to comment out the clamping part in your code to see whether your ship moves properly then.

I tried commenting out the clamping values, but it still doesn’t work. Giving the same jittery motion again

Could you please share your code as formatted text and a few relevant screenshots of what you have/did in Unity?

Actually, when commenting out the clamping part, the ship should not have a jittery motion, so I assume that there might be something wrong with the code.

void ProcessMovement() {
        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        yThrow = CrossPlatformInputManager.GetAxis("Vertical"); 

        float xOffset = xThrow * xSpeed * Time.deltaTime;
        float yOffset = yThrow * ySpeed * Time.deltaTime; 

        float RawPosx = transform.localPosition.x + xOffset;
        float RawPosy = transform.localPosition.y + yOffset;

        //float clampedXPos = Mathf.Clamp(RawPosx, -xRange, xRange);
        //float clampedYPos = Mathf.Clamp(RawPosy, -yRange, yRange);

        transform.localPosition = new Vector3(RawPosx, RawPosy, transform.localPosition.z);
    }

Here’s the code that processes player movement. I don’t know what screenshots I should share, so could you let me know about that? Thanks!

I don’t know if it helps, but I tried changing the X and Y position of the player ship from the inspector while playing the game. I can’t even change it through the inspector. Dragging over the values doesn’t work and typing a value and hitting enter just resets the position to what it was

What do you mean by that? During runtime? The position gets overridden each frame.

Could you share your entire script as formatted text? I’m wondering if ProcessMovement gets called.

Also log xOffset and yOffset into your console. What values do they have? And please share a screenshot of the Inspector of your ship in the Hierarchy.

Here’s the entire code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerControl : MonoBehaviour
{
    [Header("General")] //Used to give a heading to a group of variables in the inspector. Useful for organization. 
    [Tooltip("In ms^-1")] [SerializeField] float xSpeed = 10f;  
    [Tooltip("In ms^-1")] [SerializeField] float ySpeed = 10f; 
    [Tooltip("In m (Position to clamp movement)")] [SerializeField] float xRange = 5f;  
    [Tooltip("In m (Position to clamp movement)")] [SerializeField] float yRange = 3.5f; 
    [Tooltip("Array of both left and right guns")] [SerializeField] GameObject[] guns;
     
    [Header("Screen-Position based")]
    [SerializeField] float positionPitchFactor = -9f;
    [SerializeField] float positionYawFactor = 10f;

    [Header("Control-Throw based")]
    [SerializeField] float controlPitchFactor = -20f;
    [SerializeField] float controlRollFactor = -30f;

    float xThrow, yThrow;
    bool controlEnabled = true;

    void Update() {
        if(controlEnabled == true) {
            ProcessMovement();
            ProcessRotation();
            ProcessFiring();
        }
    }

    void OnPlayerDeath() { //Called by string reference (Rename the string in CollisionHandler if you change the name here)
        controlEnabled = false;
    }

    void ProcessMovement() {
        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        yThrow = CrossPlatformInputManager.GetAxis("Vertical"); 

        float xOffset = xThrow * xSpeed * Time.deltaTime;
        float yOffset = yThrow * ySpeed * Time.deltaTime; 

        float RawPosx = transform.localPosition.x + xOffset;
        float RawPosy = transform.localPosition.y + yOffset;

        float clampedXPos = Mathf.Clamp(RawPosx, -xRange, xRange);
        float clampedYPos = Mathf.Clamp(RawPosy, -yRange, yRange);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }

    void ProcessRotation() {
        float pitchDueToPos = transform.localPosition.y * positionPitchFactor;
        float pitchDueToRot = yThrow * controlPitchFactor;
        float Pitch = pitchDueToPos + pitchDueToRot;

        float Yaw = transform.localPosition.x * positionYawFactor; 

        float Roll = xThrow * controlRollFactor;
        transform.localRotation = Quaternion.Euler(Pitch, Yaw, Roll);
    }

    void ProcessFiring() {
        if(CrossPlatformInputManager.GetButton("Fire")) {
            ActivateGuns();
        }
        else {
            DeactivateGuns();
        }
    }

    void ActivateGuns() {
        foreach (GameObject gun in guns) {
            gun.SetActive(true);
        }
    }

    void DeactivateGuns() {
        foreach (GameObject gun in guns) {
            gun.SetActive(false);
        }
    }
}

And here’s a video clip of what I mean by the position doesn’t change: https://mega.nz/file/E4UHhYRL#hdVJTIvlEmukK0YGcew8eTAJW_1MiyOl5YSzTWHbOlY

Could you share a screenshot of your console with the values of xOffset and yOffset during runtime? Unfortunately, I do not have time to download and watch for multiple minutes just to figure out whether there is the requested information in that video. I did that twice, so this time, a screenshot would be great.

Sorry I didn’t read that part earlier. Here you go, the ss of both the logged values and the inspector of the ship:

Could you please zip your project, upload it onto the internet (e.g. Dropbox, OneDrive) and share a link here? I’d like to take a look into it. And please delete the Library folder as well as the sound files from the zip.

I deleted the Library folder and the audio from the assets. It’s a bit under 700mb. Here’s the link:

https://mega.nz/file/ElVEHAhC#zJXxRF4eAnx48Z1Kn1I8vVi18AJIVK71vNgc6k4Xn_I

I’ve just downloaded your project. The folder was so large because of the .git folder. I forgot to mention that it could be removed from the zip file as well.


I’ve checked your animation, and the child is animated. See here:

image

Click on that icon and remove the property of the child.

image

Did this fix it?

1 Like

Holy cow that fixed it! Thank you so much, you’re a life-saver

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

Privacy & Terms