Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera follow.

Discussion in 'Scripting' started by Vodka430, Mar 24, 2016.

  1. Vodka430

    Vodka430

    Joined:
    Mar 24, 2016
    Posts:
    1
    So i got this code where the camera follows my player and i really like it, but i don't really know how to make it that it does not auto rotate, but you would be able to rotate it around player with your arrow keys (up, down, left and right) any help please, here is the code:
    using UnityEngine;
    using System.Collections;

    public class CameraFollow : MonoBehaviour {

    [SerializeField]
    private float distanceAway;
    [SerializeField]
    private float distanceUp;
    [SerializeField]
    private float smooth;
    [SerializeField]
    private Transform follow;
    private Vector3 targetPosition;

    void Start ()
    {
    follow = GameObject.FindWithTag ("Player").transform;
    }

    void LateUpdate ()
    {
    targetPosition = follow.position + follow.up * distanceUp - follow.forward * distanceAway;
    transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
    transform.LookAt (follow);
    }

    }
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    Maybe transform.rotateAround. Or you could parent the camera to an empty to act as an axis point at the player position and then rotate the parent.