Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by 1Crio · Oct 22, 2015 at 11:55 AM · cameramovementtrigger

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);
 }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Oct 22, 2015 at 11:56 AM 0
Share

"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.

avatar image 1Crio · Oct 22, 2015 at 03:44 PM 0
Share

The problem is that the camera only zoom and doesn't move to the trigger. P.s target = trigger

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Oct 22, 2015 at 06:19 PM 0
Share

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;
avatar image 1Crio Statement · Oct 24, 2015 at 04:11 PM 0
Share

I converted the entire script to c#,how is it in c#?

avatar image Statement 1Crio · Oct 24, 2015 at 05:18 PM 0
Share

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.

Show more comments
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lenercopa · Feb 19, 2016 at 09:59 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges