- Home /
I want the camera, which is following my spaceship, to rotate to wherever the cursor is looking
Hi,
I am writing a very very basic space game. i have a spaceship flying around and the camera following it, but i dont want the camera to center the view on the spaceship but rather to follow the mousemovement, so i can aim and shoot at other ships and control my spaceship seperately.
here is my cameracode so far, im happy with it following my ship, but i have no idea how interacting with the mousemovement works...
using UnityEngine;
using System.Collections;
public class Camera_follow: MonoBehaviour
{
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Update()
{
Vector3 wantedPosition;
if (followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
// smooth camera focus on object
if (smoothRotation)
{
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt(target, target.up);
}
}
Your answer
Follow this Question
Related Questions
Camera following player gameObject,Camera follows player, with script attached. 1 Answer
Forward and back movements with a camera emulating an isometric view 1 Answer
MoveRotation stops working while player is moving 0 Answers
How to make the camera follow the player while still being able to be rotated? 1 Answer
How can I rotate my camera when player has rigidbody? 0 Answers