- Home /
Camera follow player while it doesnt block animations
EDIT 18-10
First of all. i did edit my explanation because i think it wasn't clear enough. let me explain the situation better. i have a simple cameracontrol script what follows the player:
public GameObject player;
private Vector3 offset;
void Start() {
offset = transform.position - player.transform.position;
}
void LateUpdate() {
transform.position = player.transform.position + offset;
}
i did attach this script to my Main Camera. and my Main Camera is a child of a empty parent. in my empty parent i have the component (legacy)Animations and with those animations i control the main camera. well here comes the problem. while my player is moving in my straight line (Z-axis), my Main camera is following but at a specific point it needs to play an animation. it animation says to go in the Y-Axis from 0 to -3 in and in the Z-Axis from localposition(thats why i use a parent) to -1. the animation works in de animation editor but in runtime it won't. i did ask did but nobody did answer in 3 days. but i actually found the problem but i dont know hot to fix it. the problem is that the cameracontrol script is blocking the animation because the script is using already transform position so it blocks the animation. i tried to animate the rotation and scale and those works perfectly except the position. the question from this story is: do you guys know how to fix it or do you guys know another way to follow the player and doesn't affect the animations. i did look to a lot of topics and forums for an answer but almost every post i saw was make it a child or use this simple code which i already tried. i hope it's is clearer now and can hopefully find an answer.
Thanks in advance
Answer by Zodiarc · Oct 18, 2016 at 01:25 PM
Why are you using animations? You could easily achieve this using some code.
Vector3 toRotation = new Vector3(0, -3, -1); // If I understood you correctly
Quaternion currentRotation = transform.rotation;
transform.rotation = Quaternion.Lerp(currentRotation, Quaternion.Euler(toRotation), Time.deltaTime * rotationSpeed);
Or if you mean position:
Vector3 toPosition = new Vector3(0, -3, -1);
transform.localPosition = Vector3.Lerp(transform.position, toPosition, speed * Time.deltaTime);
the camera is following the player with code. but at a specific point the camera needs to go in a smooth way in the Y-axis with 3 down and in the Z-axis with 1 behind. but if i use this follow code(which is already using the transform.position), than i cant use something else what uses the position. and i saw other methodes where you can use a smooth animation but im not fimilair with this methode. i am btw not a pro xD
edit: i didn't saw you did post some codes. ill try them and i also forgot to mention thanks for replying
You can still use the code i provided, but ins$$anonymous$$d on transform.position you apply it on the offset.
well i forgot to mention i have in total 2 script. 1 game script and 1 camera script. i actually need to write it in the gamescript but when i paste it in the gamscript, vector3 thinks the players needs to move with -3 and -1. is there a way to change it to the camera ins$$anonymous$$d it thinks the player needs to move? ill also poste the gamescript code. its maybe a bit messy but maybe it will help you to help me: link text
You need to create a public property for the camera transform, assign it through the inspector and work with cameraTransform.position ins$$anonymous$$d of transform.position (or whatever you call the variable)
Try this (not tested)
[System.Serializable]
public class Camera$$anonymous$$over : $$anonymous$$onoBehaviour {
public Transform playerTransform;
public Vector3 cameraOffset;
public Vector3 cameraAnimationOffset = new Vector3(0, -3, -1);
public float speed = 1.0f;
public void Update() {
if(this.playerTransform == null) {
Debug.Log("Assign a player")
}
if(//your animation condition here){
Vector3 offsetWithAnimationOffset = this.cameraOffset + this.cameraAnimationOffset;
this.transform.position = Vector3.Lerp(this.transform.position, offsetWithAnimationOffset, this.speed * Time.deltaTime);
} else {
this.transform.position = playerTransform.position + this.cameraOffset;
}
}
}
Assign the player transform and your camera offset through the inspector and see if it works.
The cameraTransform.LookAt is the issue. Delete the line and it should be fine.
IT WOR$$anonymous$$S!. thanks a lot. i was struggeling for weeks. how can i thank you for your time and effort? btw i used legacy animations to animate the rotation.
Simply accept the answer and It'll be enough. I think for camera animations (unless it's a cutscene) it's always better to use scripted animations than "baked". You are far more flexible.