1
\$\begingroup\$

I am currently working on a 2D game that gives the player the ability to drive cars and get in and out of them. I wrote a script that basically gets the job done the only issue is every time my character gets out of the car, the camera jumps from where the car originally was to where i drove it to but while im driving the car the camera follows the car just fine. The preferred reaction would be for the camera to just still be where the car is and immediately start following the player when they get out of the car. Here is my entire driving script

public class Driving : MonoBehaviour {

public int movementSpeed = 2;
public int rotationSpeed = 50;
public float angle = 0;
public int levelToLoad;
public CameraController camCon;
public PlayerController playCon;
public int powerMod;

public GameObject camera1;
public GameObject player;
public GameObject tractor1;
bool isInVehicle;
bool isInCollider;
bool gotOut;
Vector3 tractorPosition;
public float power = 3;
public float maxspeed = 10;
public float turnpower = 2;
public float friction = 3;
public Vector2 curspeed;
Rigidbody2D rbody2d;
public bool gas;

void Start()
{
    rbody2d = GetComponent<Rigidbody2D>();
    isInCollider = false;
    isInVehicle = false;
    Quaternion origRotation = camCon.transform.rotation;
    transform.rotation = Quaternion.identity ;
}

void Update()
{
    if (gotOut)
    {
        isInCollider = false;
        isInVehicle = false;
    }


    if (isInVehicle)
    {
        isInCollider = true;

        curspeed = new Vector2(rbody2d.velocity.x, rbody2d.velocity.y);

        if (curspeed.magnitude > maxspeed)
        {
            curspeed = curspeed.normalized;
            curspeed *= maxspeed;
        }

        camCon.targetSpeed = maxspeed -5;

        if (Input.GetKey(KeyCode.W))
        {
            //transform.Translate(Vector3.up * movementSpeed * Time.deltaTime);
            rbody2d.AddForce(transform.up * power);
            rbody2d.drag = friction;
            gas = true;
        }
        if (Input.GetKey(KeyCode.S))
        {
            // transform.Translate(Vector3.down * movementSpeed * Time.deltaTime);
            rbody2d.AddForce(-(transform.up) * (power/2));
            rbody2d.drag = friction;
            gas = true;
        }
        if (Input.GetKey(KeyCode.A))
        {
            //angle = angle + rotationSpeed * Time.deltaTime;
            transform.Rotate(Vector3.forward * turnpower);
        }   //noGas();
        if (Input.GetKey(KeyCode.D))
        {
            //angle = angle - rotationSpeed * Time.deltaTime;
            transform.Rotate(Vector3.forward * -turnpower);
            //noGas();
        }
        //transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
        noGas();


    }
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        //Application.LoadLevel(0);
        SceneManager.LoadScene(levelToLoad);
        //transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
        Transform thistrans1 = transform;
        ResetRot(thistrans1);
    }
    if (Input.GetKeyDown(KeyCode.E))
    {

        if (gotOut)
        {
            gotOut = false;
            //transform.rotation = Quaternion.identity;
        }

        if (isInCollider)
        //if (!isInVehicle)
        {
            Debug.Log("Key pressed...");
            //camera1 = GameObject.FindWithTag("MainCamera");
            //player = GameObject.FindWithTag("player");
            //tractor1 = GameObject.FindWithTag("tractor1");
            tractorPosition = transform.position;
            if (!isInVehicle)
            {
                Debug.Log("Entered vehicle..");
                //camera1.transform.parent = tractor1.transform;
                player.GetComponent<Renderer>().enabled = false;
                isInVehicle = true;
                camCon.cameraTarget = tractor1;
                playCon.moveSpeed = 0;
                camCon.targetSpeed = 10;

            }
            else
            {
                Debug.Log("In vehicle....");
                if (isInVehicle)
                {
                    //Transform thistrans1 = transform;
                    //ResetRot(thistrans1);
                    Debug.Log("Exited vehicle....");
                    //camera1.transform.parent = player.transform;
                    player.GetComponent<Renderer>().enabled = true;
                    player.transform.position = tractorPosition;
                    isInVehicle = false;
                    isInCollider = false;
                    gotOut = true;
                    camCon.cameraTarget = player;
                    playCon.moveSpeed = 3f;
                    //camCon.transform.rotation = (player.transform.rotation);
                    camCon.transform.position = new Vector3(player.transform.position.x, camCon.transform.position.y, player.transform.position.z);
                    camCon.targetSpeed = playCon.moveSpeed;
                }
            }
            camera1.transform.position = new Vector3(0f, 0f, -10f);
            camera1.transform.rotation = player.transform.rotation;
        }

    }
}

public virtual void ResetRot(Transform trans)
{
    if (isInVehicle)
    {
        trans.rotation = Quaternion.identity;
    }

}

void OnTriggerStay2D(Collider2D col)
{
    if(col.name == "Player")
    {
        Debug.Log("Trigger entered....");
        isInCollider = true;
    }

}

private void OnTriggerExit2D(Collider2D collision)
{
    isInCollider = false;
    //isInVehicle = false;
}

void noGas()
{
    //bool gas;
    if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
    {
        gas = true;
        turnpower = (1 * powerMod);
    }
    else
    {
        gas = false;
        turnpower = 0;
    }

    if (!gas)
    {
        rbody2d.drag = friction * 2;

    }
}

}

ANY help would be greatly appreciated

\$\endgroup\$
0
\$\begingroup\$

You're explicitly setting the camera position back to <0,0,-10> when the player exits the vehicle

camera1.transform.position = new Vector3(0f, 0f, -10f);

But since the target of the camera is still the player (from when the player initially entered the vehicle) the camera controller is moving the camera back to the current location of the player.

I'd suggest try removing that line altogether and retesting; but if you still don't get the correct behavior, try changing it to

camera1.transform.position = new Vector3(player.transform.position);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.