0

I am working on some kind of temple run game project just for the educational purpose. And I have created a script which should focus on character from little bit high and then change its position to characters. Like in temple run when the camera slowly lerps toward the player when the game scripts. Here is my script

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

public class CameraMotor : MonoBehaviour {
    private Vector3 ofset;
    private Vector3 characterposition;

    private float transition =0.0f;
    private float animationduration = 2.0f;
    private Vector3 animationoffset = new Vector3(0,2f,2f);
    void Start () {
        ofset = GameObject.FindObjectOfType<CharacterController> ().transform.position - this.transform.position;
    }

    // Update is called once per frame
    void Update () {
        characterposition = GameObject.FindObjectOfType<CharacterController> ().transform.position - ofset;
        characterposition.x = 1.3f;
        characterposition.y = Mathf.Clamp (characterposition.y, 3f, 5f);
        if (transition > 1.0f) {
            this.transform.position = characterposition;
        } else {
            this.transform.position = Vector3.Lerp (characterposition + animationoffset, characterposition, transition);
            transition =  Time.deltaTime* 1 / animationduration;
            this.transform.LookAt (characterposition, Vector3.up);
        }

    }
}

2 Answers 2

1

Following line is wrong

this.transform.LookAt (characterposition, Vector3.up);

LookAt() method requires Transform type parameter, not Vector3. Try use these

private Vector3 ofset;
private Vector3 characterposition;
private CharacterController characterCon;

private float transition =0.0f;
private float animationduration = 2.0f;
private Vector3 animationoffset = new Vector3(0,2f,2f);

void Start () {
    ofset = GameObject.FindObjectOfType<CharacterController> ().transform.position - this.transform.position;
    characterCon = GameObject.FindObjectOfType<CharacterController> ().GetComponent<CharacterController>();
}

void Update () {

    characterposition = characterCon.transform.position - ofset;
    characterposition.x = 1.3f;
    characterposition.y = Mathf.Clamp (characterposition.y, 3f, 5f);
    if (transition > 1.0f) {
        this.transform.position = characterposition;
    } else {
        this.transform.position = Vector3.Lerp (characterposition + animationoffset, characterposition, transition);
        transition =  Time.deltaTime * 1 / animationduration;
    }

    this.transform.LookAt (characterCon.transform, Vector3.up);
}

Also you calling FindObjectOfType method at every frame causes poor performances. Take a look at following link LookAt

Sign up to request clarification or add additional context in comments.

Comments

0

You are close but your transition value needs to continually increase for your Lerp to work properly. Try changing this line:

transition =  Time.deltaTime* 1 / animationduration;

to

transition +=  Time.deltaTime* 1 / animationduration;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.