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