Trying to adjust Y position, ship does not appear correctly

Hi,

Could you anyone provide me some help. I am trying to add clampedYpos (changing x and y positions). I want to control the height. Yet, my ship does not appear to be right as shown in picture 1

But if I only use clampedXpos ( I left the y and z unchanged as shown in the tutorial), it does not have any issue. the ship appear correctly as shown below

Here is my code

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

public class Player : MonoBehaviour
{
    [Tooltip("In ms^-1")] [SerializeField] float xSpeed = 4f; // meter per second
    [Tooltip("In ms^-1")] [SerializeField] float ySpeed = 4f; // meter per second
    [Tooltip("In m")] [SerializeField] float xRange= 6f; // meter per second
    [Tooltip("In m")] [SerializeField] float yRange = 12f; // meter per second

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

    // Update is called once per frame
    void Update()
    {
        // Adjusting Horizontal axis
        float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        float xOffset = xThrow * xSpeed * Time.deltaTime;
        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        // Adjusting Vertical axis
        float yThrow = CrossPlatformInputManager.GetAxis("Vertical");
        float yOffset = yThrow * ySpeed * Time.deltaTime;
        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

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

Thanks for your help

Hi Ihshan,

Maybe the clamp values in your Inspector are wrong. Move the space ship in your editor on the y-axis to the edges of your game screen to figure out what clamp values you need.

1 Like

Hello Nina,

Thanks. It works now

Cheers,
IG

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

Privacy & Terms