1
\$\begingroup\$

I want to rotate the camera around the player by pressing E and Q but it doesnt rotates around the player. I tried RotateAround but didnt worked.

Code:


public Transform player;
public float smoothSpeed = 0.125f;
public float rotateSpeed;
public Vector3 offset;
private Vector3 velocity = Vector3.zero;


void LateUpdate () 
{
    transform.position = Vector3.SmoothDamp(transform.position, player.position + offset, ref velocity, smoothSpeed * Time.deltaTime);
}

void Update()
{
    if(Input.GetKey (KeyCode.E))
    {
        transform.RotateAround(player.position, Vector3.up, rotateSpeed * Time.deltaTime);
    }

    if(Input.GetKey (KeyCode.Q))
    {
        transform.RotateAround(player.position, -Vector3.up, rotateSpeed * Time.deltaTime);
    }
}

}

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

Updating the transform.position inside LateUpdate() function overrides the effects of RotateAround() inside Update().

RotateAround() actually modifies the position of the transform, hence, when you do the SmoothDamp() you are negating the effects of RotateAround().

My suggestion would be to insert an empty game object into the hierarchy.

Camera Jig <— Main Camera

Then create two separate scripts:

CameraFollow.cs ( Add this to the Camera Jig gameobject )

public Transform player;
public float smoothSpeed = 0.125f;
public Vector3 offset;
private Vector3 velocity = Vector3.zero;

void LateUpdate () 
{
    transform.position = Vector3.SmoothDamp(transform.position, player.position + offset, ref velocity, smoothSpeed * Time.deltaTime);
}

CameraOrbit.cs ( Add to the Main Camera gameobject )

public Transform cameraJig;
public float rotateSpeed;

void LateUpdate()
{
    if(Input.GetKey (KeyCode.E))
    {
        transform.RotateAround(cameraJig.position, Vector3.up, rotateSpeed * Time.deltaTime);
    }

    if(Input.GetKey (KeyCode.Q))
    {
        transform.RotateAround(cameraJig.position, -Vector3.up, rotateSpeed * Time.deltaTime);
    }
}

So in the end the Camera Jig is positioned where the player is and the Camera rotates around the Jig.

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

This might help you,Assign this code to your camera

[Range (0.00001f,3)]
public float speed = 0.1f;


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () 
{
    Vector3 velocity = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")) * speed;
    transform.Translate (velocity);

    //Rotation
    float rotation =0;
    if (Input.GetKey (KeyCode.Q))
        rotation -= 1;
    if (Input.GetKey (KeyCode.E))
        rotation += 1;
    transform.Rotate (0, rotation, 0);
}

use Q or E button for rotation.

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

What you could use is an orbit camera. Here is an example:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour {

public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;

public float yMinLimit = -20f;
public float yMaxLimit = 80f;

public float distanceMin = .5f;
public float distanceMax = 15f;

private Rigidbody rigidbody;

float x = 0.0f;
float y = 0.0f;

// Use this for initialization
void Start () 
{
    Vector3 angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    rigidbody = GetComponent<Rigidbody>();

    // Make the rigid body not change rotation
    if (rigidbody != null)
    {
        rigidbody.freezeRotation = true;
    }
}

void LateUpdate () 
{
    if (target) 
    {
        x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        Quaternion rotation = Quaternion.Euler(y, x, 0);

        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);

        RaycastHit hit;
        if (Physics.Linecast (target.position, transform.position, out hit)) 
        {
            distance -=  hit.distance;
        }
        Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
        Vector3 position = rotation * negDistance + target.position;

        transform.rotation = rotation;
        transform.position = position;
    }
}

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ This would work for other purposes but not for mine, because I am using a click to move system so I want just to rotate around X Axis with Keys. But still thanks. \$\endgroup\$
    – Daniel
    Feb 11, 2018 at 22:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .