Punching Argon Assault Ship in the FACE!

Hey guys/gals.

Been working my way through the program but I hit a wall. My ship is going absolutely nuts once I introduce the Mathf.Clamp function into the code. I recorded a video so you can see what I mean here:

The ship just keeps spinning around on the y axis as it goes around the circuit whenever i introduce the Mathf.Clamp function.

Here’s my code, do you see anything off?


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

public class ShipController : MonoBehaviour
{
[Tooltip(“in ms^-1”)][SerializeField] float xSpeed = 1f;
[Tooltip(“in m”)] [SerializeField] float xRange = 5f;

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

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

    float xOffset = xThrow * xSpeed * Time.deltaTime;

    float rawNewXPos = transform.localPosition.x + xOffset;

    float clampXpos = Mathf.Clamp(rawNewXPos, -xRange, xRange);


    transform.localPosition = new Vector3(rawNewXPos, transform.localPosition.y, transform.localPosition.z);

    

}

}


Thank you for your time. Your help with save the screen of a 27" iMac from complete destruction.

Hi Ken,

Welcome to our community! :slight_smile:

Unfortunately, I cannot watch the video. It’s not working for me. How did you figure out the xRange value?

Thanks for the Reply Nina! I re-uploaded the video to YouTube.

Try this: https://youtu.be/yiifUKFF-RM

Basically what is happening is as soon as I introduce the Mathf.Clamp restricted version of the variable the ship goes haywire. It does this for both x and y access. I calculated xRange of 5 just by how much I wanted the ship to move in restriction. I was planning to adjust that later to 10 or 15.

Thank you for your time

Thank you for the video. It looks as if the ship controller does not control the ship (child) but the camera (parent). Your code is supposed to move the ship/child only. Try to remove the ShipController from the MainCamera game object and assign it to the SpaceShip game object. That should hopefully fix the issue. If not, please let me know.

YES! That was is. Thank you for your help. I would have never found that. I appreciate you

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

Privacy & Terms