Rail shooter, Stange Y axis behaviour

Hi guys,
I am working on my own take on Argon Assault (love the course, by the way!)

I have a strange problem with the control scripts! I can get the X Axis movements to work fine, but EVERY time I try to implement Y axis movement, I get strange behaviours such as the ship not keeping up with the camera…

here is the script…if someone can spot the glaring error, I’d surely appreciate it!

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

public class gyroplayer : MonoBehaviour
{
[Tooltip(“In meters per second”)][SerializeField] float speed = 4f;
[Tooltip(“In meters”)] [SerializeField] float xRange = 10f;
[Tooltip(“In meters”)] [SerializeField] float yRange = 3f;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    float yThrow = CrossPlatformInputManager.GetAxis("Vertical");

    float xOffset = xThrow * speed * Time.deltaTime;
    float yOffset = yThrow * speed * Time.deltaTime;

    float rawXPos = transform.localPosition.x + xOffset;
    float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

    float rawYPos = transform.localPosition.y + yOffset;
    float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

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

}

Update…the code seems to move my gyrocopter to ABOVE the camera, instead of sitting nicely in front of it…the code for x-axis movement doesn’t do this…so I’m confused, as there is very little difference in the two parts of the code…

GYRO

Hi Andy,

What are your range values? Check them in your Inspector.

Hi Nina, that occurred to me literally after I posted this and looked at my post :smiley: I have moved the player control empty relative to the camera, and I got the result I wanted, thanks :smiley:

I think you can try instead of moving the ship by itself directly try this.
Create empty game object, and put on it control movement by x and y axis.
Create script on a ship that only can follow empty object by x and y axis. z axis is unchanged. Also make ship transform look at empty game object transform.
This i think will solve weird rotation behavior.

Does that mean the issue is fixed? :slight_smile:


See also:

Hi Nina,
yes problem solved, happy new year!

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

Privacy & Terms