0
\$\begingroup\$

This is a screenshot showing the camera position. I want it to rotate slowly smooth to the left and once looking at the soldier to stop rotating.

Cut scene

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierNaviFight : MonoBehaviour
{
    public Camera fightCam;
    public Transform target;
    public float speed = 2f;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {

    }
}

The script is attached to a empty gameobject. What I want to do is once the camera stopped rotating and looking at the soldier to make the soldier start moving to the right and camera should follow the soldier rotating and keep follow the soldier moving.

The main idea is to create a cut scene. The camera is position at this point just to display the cut scene.

To make the first rotation to the target I tried this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierNaviFight : MonoBehaviour
{
    public Camera fightCam;
    public Transform target;
    public float speed = 2f;
    Quaternion targetRotation;

    // Use this for initialization
    void Start ()
    {
        targetRotation = target.rotation;
    }

    // Update is called once per frame
    void Update ()
    {
        Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * speed);
    }
}

But the camera is not rotating at all.

This is screenshot of the Hierarchy and Inspector:

The script is attached to GameObject: SoldierNaviFight The camera that I want to move is the child of SoldierNaviFight: SoldierNaviFightCamera

Hierarchy

UPDATE:

This will rotate the camera but it's rotating it to the right instead to the left to the soldier. Not sure why:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierNaviFight : MonoBehaviour
{
    public Camera fightCam;
    public Transform target;
    public float speed = 2f;
    Quaternion targetRotation;

    // Use this for initialization
    void Start ()
    {
        targetRotation = target.rotation;
    }

    // Update is called once per frame
    void Update ()
    {
        fightCam.transform.rotation = Quaternion.RotateTowards(fightCam.transform.rotation, targetRotation, Time.deltaTime * speed);
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

So first off, you need to seperate this kind of logic, otherwise you'll face problems along the way. I would suggest to you, to create a small script, that handles the cut scene stuff, deactivates and than activates unity ready-to-use third person camera setup. Otherwise, if you want to use your own camera script, activate this one instead.

First, create a boolean that checks what kind of status our cutscene has:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierNaviFight : MonoBehaviour
{
    bool CutSceneStatus = false;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {

    }
}

To manipulate the camera rotation and position, use transform and Rotate and multiply those by a float and time (almost like you already tried to do)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierNaviFight : MonoBehaviour
{
    bool CutSceneStatus = false;

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
 CheckIfPlayerVisible();
 TransformCamera();
 RotateCamera();
}

void TransformCamera(){

 if(!CutSceneStatus){
 transform.position += new Vector3(30, 0, 0) * speed * Time.deltaTime;
 }
}

void RotateCamera(){
 if(!CutSceneStatus){
 transform.Rotate(0, 30, 0) * Time.deltaTime;
 }
}

void CheckIfPlayerVisible(){

 RaycastHit hit;
    // Does the ray intersect any objects excluding the player layer
    if (Physics.Raycast(transform.position, 
    transform.TransformDirection(Vector3.forward), out hit, 
    Mathf.Infinity))
    {
        if(hit.gameObject.tag == "Player"){
        Debug.DrawRay(transform.position, 
        transform.TransformDirection(Vector3.forward) * hit.distance, 
        Color.yellow);
        Debug.Log("Did Hit Player!");

        CutSceneStatus = true;
        }
    }
    else
    {
        Debug.DrawRay(transform.position, 
        transform.TransformDirection(Vector3.forward) * 1000, Color.white);
        Debug.Log("Did not Hit");
        }


   void DeactivateScript(){

    if(CutSceneStatus = true){

      Destroy(gameObject, 1f);
      }

   }
}

Note that this can only work if your Character has a Tag "Player" and if the camera has a Raycast component attached to it. Of course, the given values for the rotation, transformations are just examples and need to be adjusted for your scene. Also, the camera needs to be at a height where it can "hit" the player. This is not the most elegant way, but just to give you an idea how to approach this. When you 'destroy' the camera itself, you could also activate the third person camera from unity or your very own camera.

Also, check out Unitys CutScene Guide, im pretty sure they added lately a new function to handle cut scenes without any coding. See this link here

Greetings!

\$\endgroup\$
2
  • \$\begingroup\$ The link you provided is great stuff. I like it. Thanks for solution script idea. \$\endgroup\$
    – Daniel Lip
    Commented May 30, 2018 at 19:21
  • \$\begingroup\$ I've seen your update. Try to minus one of the values fightCam.transform.rotation, -targetRotation or the first one. Should to the job. \$\endgroup\$
    – Prometheus
    Commented May 31, 2018 at 8:08

You must log in to answer this question.

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