0

I'm making the SpaceShooter game in Unity.

Here is the function.

 void FixedUpdate ()
{

    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");


    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    Player.GetComponent<Rigidbody>().velocity=movement*speed;

    Player.GetComponent<Rigidbody> ().rotation = Quaternion.Euler(0.0f,0.0f,Player.GetComponent<Rigidbody> ().velocity.x * -tilt);
}

In the above code when I add the Clamp function to limit the Ship to the boundaries, the Ship size reduces from the sides. This is the Clamp function code that reduces the size of the ship.

 Player.GetComponent<Rigidbody> ().position=new Vector3 
    (
        Mathf.Clamp(Player.GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
        0.0f,
        Mathf.Clamp(Player.GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    );

Ship size Before: enter image description here

Ship size After: enter image description here

I've given the screen boundary values accurately in the Clamping funtion.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Arif Khan
  • 97
  • 1
  • 1
  • 8

2 Answers2

2

Modifying the position of the Rigidbody indeed moves the Transform, but after the last physic step. While it is considered faster, it may cause some undesired behaviour, as you are moving something the physic of the engine is also using, and doing it in the FixedUpdate function which should mostly be used for Physics matter. Replace Player.GetComponent<Rigidbody>().position with Player.GetComponent<Transform>().position. Also, as you are not using any physic here but you are instead modifying an object position directly, set this part of the code in the Update function instead.

void Update()
{
    Transform playerTransform = Player.GetComponent<Transform>();

    playerTransform.position=new Vector3 
    (
        Mathf.Clamp(playerTransform.position.x, boundary.xMin, boundary.xMax),
        0.0f,
        Mathf.Clamp(playerTransform.position.z, boundary.zMin, boundary.zMax)
    );
}

If you ever want to move an object using its Rigidbody, never do it this way by the way. Consider using the MovePosition function instead. Moving it the way you did cause it to teleport and not compute collisions on the same frame, but on the next one.

As a side note, you should save a reference to the player's transform in your class, so you don't have to use the GetComponent function everytime.

Izuka
  • 2,572
  • 5
  • 29
  • 34
  • I always thought that `GetComponent().position` and `GetComponent().position` are modifying the-same variable. Maybe, I am wrong. – Programmer Sep 26 '17 at 08:30
  • Translating an object with any physics component is never a good idea. Whenever you're dealing with colliders and other physics parts of Unity engine you should use `Rigidbody` component to translate and rotate your object. This has been discussed many times now. – mrogal.ski Sep 26 '17 at 09:53
0

Solved the problem by pushing the background "quad" beneath (about -4) the plane and set the ship's Y value to 0

Arif Khan
  • 97
  • 1
  • 1
  • 8