Ship moving to origin instead of using local position

Hello, at a loss here, hoping for some help.

Argon Assault - Mathf.Clamp() video

I am following along with Rick but running into a problem when I try to implement the Mathf.Clamp() portion. When I do this and Play, my ship teleports to the origin of the scene (plus or minus the xRange & yRange values). It doesn’t seem to be using the ship’s local position to make the change, it teleports to origin and starts the run from there.

I have a feeling it has something to do with the prefab but not sure. I looked into resetting the prefab’s origin but in the prefab itself its set to (0,0,0). I also tried using a totally different prefab and it still teleports to the origin first.

While troubleshooting I threw out all my code and tried using Rick’s but it still does this. Just for reference here’s Rick’s code that’s also not working for me:

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

public class PlayerControls : MonoBehaviour
    [SerializeField] float controlSpeed = 10f;
    [SerializeField] float xRange = 10f;
    [SerializeField] float yRange = 7f;

    void Update()
        float xThrow = Input.GetAxis("Horizontal");
        float yThrow = Input.GetAxis("Vertical");

        float xOffset = xThrow * Time.deltaTime * controlSpeed;
        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        float yOffset = yThrow * Time.deltaTime * controlSpeed;
        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

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

If you’re showing Rick’s code in your post, I feel like I’m going to need more context here. I need to see the code you’re trying to run, which if you have changed it to match Rick’s exactly, then that’s fine. I also need to see your hierarchy, and how you have things set up. Maybe a screenshot of the prefab would be helpful as well.
I’m taking a pure shot in the dark and I’m probably wrong, but from the behavior you’re describing I would expect the issue to be that your player isn’t childed to the PlayerRig empty Rick had us create during Argon Assault, and instead is its own thing on the hierarchy, thus local space for it would be world space.

Thanks for taking a look. One thing I forgot to mention is that everything worked fine before the addition of Mathf.Clamp(). If I load rawXPos and rawYPos into the new transform Vector3, it works fine (aside from the ship being able to fly off because there’s no clamp).


Prefab properties:

The position the Properties currently show is the correct local position (start of the timeline). The moment I hit Play this is overwritten by the origin +/- xRange and yRange.

What’s strange to me as that your PlayerShip child of PlayerRig is using World Space in the editor as well as in play mode despite being childed to another object. Have you been animating the PlayerRig or the PlayerShip in Timeline?
I’d recommend resetting the PlayerShip’s transform, which should make it 0,0,0 in your scene relative to its parent and see what happens then.

The Player Ship using World Space was the answer. The reset didn’t fix it but I ended up restarting from the Player Rig creation step, now the Player Ship is showing the correct child coordinates and everything is working. I must have done something weird when I added the Player Ship to the Game Rig.

Thanks for the assist!

And Happy Birthday!

Thank you! And you’re welcome. It’s not your fault, sometimes these things happen and if you don’t know exactly what you might need to be looking for it can really cause a headache. I’m just glad I was able to help you.

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

Privacy & Terms