Camera moving OnTriggerEnter
I'm trying to make a Camera that moves to a trigger (with y and z axis locked) when the player hits it but this script doesn't work:
var target : Transform;
var Camera : Transform;
var Speed : float = 1.0f;
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.tag == "Player")
{
Camera.transform.position = Vector3.Lerp(Camera.position, target.position, Time.deltaTime * Speed);
}
}
"Doesn't work" is not useful information. Describe the expected behavior, describe the behavior you're seeing from your code. And please format your code using the 101010 button.
The problem is that the camera only zoom and doesn't move to the trigger. P.s target = trigger
Answer by 1Crio · Oct 22, 2015 at 04:24 PM
I found the solution, now it works:
private var trigger : Transform;
private var Camera : Transform;
private var isLerp : boolean = false;
var Speed : float = 1.0f;
function Awake()
{
trigger = GameObject.FindGameObjectWithTag ("Trigger").transform;
Camera = GameObject.FindGameObjectWithTag ("MainCamera").transform;
}
function Update()
{
if(isLerp)
PositionChanging();
}
function PositionChanging ()
{
Camera.transform.position = Vector3.Lerp(Camera.position, trigger.position, Time.deltaTime * Speed);
}
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.tag == "Player")
{
isLerp=true;
}
}
How can i Lock y and z Axis?
How can i Lock y and z Axis?
var pos = Camera.position;
pos.x = $$anonymous$$athf.Lerp(pos.x, trigger.position.x, Time.deltaTime * Speed);
Camera.position = pos;
I converted the entire script to c#,how is it in c#?
The only difference is that C# doesn't allow you to assign Camera.position.x because Camera.position return a copy of the position. JS implicitly get the postition as a temp, modifies the temp, then sets it back.
I updated my comment to work with both C# and JS.
Answer by lenercopa · Feb 19, 2016 at 09:36 AM
i tried 1Crio's script, and t works beautifully for what i am trying to accomplish, a problem is that i have 2 triggers with this script only one is named something different and uses an identical script with a different "trigger" tag. but that causes the camera to move between a point between the 2 positions i want the camera to be in. it seems like its because the scripts are both trying to move the camera to both points at once. is there a way to make it so that doesnt happen?
Never$$anonymous$$d i figured it out, just had to add and extra few lines after the rest
function OnTriggerExit (other : Collider)
{
if(other.gameObject.tag == "Player")
{
isLerp=false;
}
}
Your answer
Follow this Question
Related Questions
Stuttering nav Mesh Agent movement when i move the character 0 Answers
Stepping on game object moves quickly to react 0 Answers
Rigidbody movement relative to third person camera 0 Answers
Character movement relative to camera doesn't work the way I intended. 1 Answer
An odd behaviour where when i rotate my camera my player starts to move 0 Answers