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. Dismiss Notice

Camera Controller, rotation issue. Have a minute to spare?

Discussion in 'Scripting' started by New-Souls, Mar 12, 2016.

  1. New-Souls

    New-Souls

    Joined:
    Jan 16, 2016
    Posts:
    3
    Hee guys,

    I just recently got into scripting and get around so far, just finished my character controller.
    But now my camera controller script is not doing as I would expect.
    There is something wrong in my 'rotation' , its missing definition to call from, somewhere.
    But I just can't figure it out. Anybody that has a spare moment to help me out?
    Cant be that much, probably just missing a line or quaternion somewhere.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.     public Transform target;
    6.     public float lookSmooth = 0.09f;
    7.     public Vector3 offsetFromTarget = new Vector3 (0, 6, -8);
    8.     public float xTilt = 10;
    9.  
    10.     Vector3 destination = Vector3.zero;
    11.     CharacterController charController;
    12.     float rotateVel = 0;
    13.  
    14.     void Start()
    15.     {
    16.         SetCameraTarget (target);
    17.     }
    18.  
    19.     void SetCameraTarget(Transform t)
    20.     {
    21.         target = t;
    22.  
    23.         if (target != null)
    24.         {
    25.             if (target.GetComponent<CharacterController> ()) {
    26.                 charController = target.GetComponent<CharacterController> ();
    27.             } else
    28.                 Debug.LogError ("The camera's target has no character controller.");
    29.         }
    30.         else
    31.             Debug.LogError ("Your Camera needs a target");
    32.     }
    33.  
    34.     void LateUpdate()
    35.     {
    36.         MoveToTarget ();
    37.         LookAtTarget ();
    38.  
    39.     }
    40.         void MoveToTarget()
    41.         {
    42.         destination = charController.TargetRotation * offsetFromTarget;
    43.         destination += target.position;
    44.         transform.position = destination;
    45.         }
    46.  
    47.         void LookAtTarget()
    48.     {
    49.         float eulerYAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref rotateVel, lookSmooth);
    50.         transform.rotation = Quaternion.Euler(transform.eulerAngles.x, eulerYAngle, 0);
    51.     }
    52.  
    - the TargetRotation in
    void MoveToTarget()
    Is not recognized somehow.

    Hopefully someone sees the error in my script.
    Thanks in advance!
     
  2. New-Souls

    New-Souls

    Joined:
    Jan 16, 2016
    Posts:
    3
  3. gremlinski

    gremlinski

    Joined:
    Jun 14, 2013
    Posts:
    4
  4. New-Souls

    New-Souls

    Joined:
    Jan 16, 2016
    Posts:
    3
    I want the camera to rotate around the character whilst looking at it.
    Instead of just rotating around the Z axis and viewing the world around the character as it rotates.
     
  5. gremlinski

    gremlinski

    Joined:
    Jun 14, 2013
    Posts:
    4
    I would put the camera in a holder object, then offset the camera on X or Z axis off center, set it to look at the target and make the camera holder have the same position as the character and rotate it on Y axis.
     
  6. mepayam

    mepayam

    Joined:
    Aug 31, 2017
    Posts:
    1
    ok
    it is a problem because of there is a another script that belongs to the Character.
    forget it
    let`s fix it:
    Add these two var before the Void Start :

    public float closeT = 2; // increase this value the camera closes to Target(character)
    Vector3 myDestination = Vector3.zero;

    then in Void MoveToTarget

    myDestination = offsetFromTarget / closeT ;
    destination = target.rotation * myDestination;
    // this is your problem
    //destination = charController.TargetRotaion * offsetFromTarget;
    destination += target.position;
    transform.position = destination;

    keep that mind you can change the base position of camera with changing th "closeTo" .

    GoodLuck