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 /
avatar image
0
Question by Omwro · Oct 14, 2016 at 09:11 AM · c#animationcameratransform.positioncamera follow

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

Comment
Add comment
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

1 Reply

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

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);


Comment
Add comment · Show 16 · 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 Omwro · Oct 18, 2016 at 01:34 PM 0
Share

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

avatar image Zodiarc Omwro · Oct 18, 2016 at 01:42 PM 1
Share

You can still use the code i provided, but ins$$anonymous$$d on transform.position you apply it on the offset.

avatar image Omwro Zodiarc · Oct 18, 2016 at 01:55 PM 0
Share

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

avatar image Zodiarc Omwro · Oct 19, 2016 at 06:34 AM 1
Share

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)

avatar image Zodiarc Omwro · Oct 19, 2016 at 09:21 AM 1
Share

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.

Show more comments
avatar image Zodiarc · Oct 20, 2016 at 02:35 PM 1
Share

The cameraTransform.LookAt is the issue. Delete the line and it should be fine.

avatar image Omwro Zodiarc · Oct 20, 2016 at 02:54 PM 0
Share

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.

avatar image Zodiarc Omwro · Oct 20, 2016 at 02:57 PM 0
Share

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.

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

77 People are following this question.

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

Related Questions

Unity Camera and transform? 1 Answer

"Lerp" back to original position after animation. 0 Answers

Question about colours of materials and lightning 1 Answer

Choppy camera follow. 1 Answer

I'm clueless about how to make my script change its position based on how close it is to the wall.. 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