-2

I have asked one question few hours ago,but I think that question not clearly define to you that what i want.So therefore i have post again it with some pictures. please carefully see the pictures.spaceship image with rotation and position

So my question is that. [Question 1]: when i press the leftarrow button then the rotation and direction of ship should be changed like as circle (3) that show in picture,and when i released the leftarrow button it should back to its rotation that show in circle (1).

[Question 2]: when i press rightarrow button then the rotation and direction of ship should be changed like as circle (2) that show in picture,and when i released the rightarrow button it should back to its rotation that show in circle (1).

transform.position += transform.forward * Time.deltaTime * 10f;
if(Input.GetKey(KeyCode.LeftArrow))
{
//code
}

if(Input.GetKey(KeyCode.RightArrow))
{
//code
}

please help me i have stuck there :(

Faisalmirza Mirza
  • 47
  • 1
  • 2
  • 10

1 Answers1

0

Have you checked out the space shooter tutorial provided by Unity? https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial/moving-player?playlist=17147

It is coded in an older version of Unity (Unity version 4), but they check to make sure it is compatible with newer versions (as of today Unity version 5.1). I've copied the code they use in the video, should the link change.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;

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

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;

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

        rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
}

The rigidBody.rotation would be for the tilt/bank illustrated in the picture you privided, but will not currently change the ship's forward rotation. pressing the left and right arrow keys would affect the rigid body velocity and causing the tilt, and once released would revert to the center ship position.

DarceVader
  • 98
  • 7
  • Thanks for your response.i have already watch your video, but i am not using rigidbody for moving the ship i am using [ transform.position += transform.forward * Time.deltaTime * 10f; ] and i aslo write a script through which i can change forward direction by pressing ArrowLeft and ArrowRight. and now i just want [tilt] .i have try many things but still i am stuck here please provide any solution :( – Faisalmirza Mirza Jan 19 '19 at 18:43