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 neo1998x · Mar 30, 2016 at 09:05 AM · programmingprogramming-basics

How does this part of the script make sense?

Sorry I'm new to programming. Please forgive me if this question is stupid.

I found this in the 2D UFO Tutorial.

using UnityEngine; using System.Collections;

public class CompleteCameraController : MonoBehaviour {

 public GameObject player;       //Public variable to store a reference to the player game object


 private Vector3 offset;         //Private variable to store the offset distance between the player and camera

 // Use this for initialization
 void Start () 
 {
     //Calculate and store the offset value by getting the distance between the player's position and camera's position.
     offset = transform.position - player.transform.position;
 }
 
 // LateUpdate is called after Update each frame
 void LateUpdate () 
 {
     // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
     transform.position = player.transform.position + offset;
 }

}

Ok if, offset = transform.position - player.transform.position; AND transform.position = player.transform.position + offset;

which means transform.position = player.transform.position + transform.position - player.transform.position;

simplify it we got player.transform.position=player.transform.position;

Yes.. Equation is correct, but what??

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

3 Replies

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

Answer by Eno-Khaon · Mar 30, 2016 at 10:19 AM

Let's take a visual approach to this:

Stickman Doodles! First, you have your character and camera and Vector2(5, -3) and Vector2(16, 2) respectively.

The difference between those two points is a subtraction of one from the other.

A ----------------> B

To get the Vector going from point A to point B, you subtract A from B. In other words, AtoB = B - A.

In the context of the drawing, that means your camera's offset from the player is (16, 2) - (5, -3), resulting in a difference of (11, 5). So, at all times, this camera should remain 11 units to the right of, and 5 units above the player.

The Start() function in Unity runs once per object, when it first enters the game. This gives the opportunity to generate that offset based on the player's and camera's starting positions without being influenced by their locations later.

The LateUpdate() function, much like the Update() function, takes place once per frame of gameplay. Because of this, your player's position will likely be changing rapidly. However, you want the camera to remain that same distance away from the player at all times. That's why we determine what its offset should be in the Start() function.

All in all, it's actually nothing more than a simple oversight.

 transform.position = player.transform.position + transform.position - player.transform.position;

is not "wrong", but it's not right either. What's actually happening is more like this:

 transform.position = player.transform.position + (transform.originalPosition + player.transform.originalPosition);

It would be silly and ineffective to recalculate the camera's offset on every frame of gameplay. Likewise, because it's NOT updating every frame, it is a constant value to make use of instead.

I hope this answered your question adequately. If not, there's always room for more doodles!

Bats? I hate bats!


example2.png (14.7 kB)
example1.png (14.0 kB)
Comment
Add comment · Show 2 · 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 neo1998x · Mar 30, 2016 at 02:47 PM 0
Share

Oh I got this now. So in Void Start() the offset is calculated, but the position in the Void Start() won't be updated. So actually the positions in Void Start() and Void LateUpdate() are different. Thanks!

And may I ask why use LateUpdate() ? The tutor in that video said because LateUpdate() make sure to execute after Update(), but there is nothing in the Update() is there? So why do we need LateUpdate() anyway?

avatar image Eno-Khaon neo1998x · Mar 30, 2016 at 04:55 PM 1
Share

LateUpdate() is used as something of a safety precaution.

Every script on every object in your scene will run through their Update() function. Then, after every object has done so, they'll move on to LateUpdate().

http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

The idea behind putting the camera positioning into LateUpdate() is that you would handle most non-physics-engine character movements and adjustments in Update(), so you're making sure that everything is done and in place and ready before telling the camera what to look at, without luck beco$$anonymous$$g a factor.

If the objects happen to run through their Update() functions in the wrong order for what you need, you could wind up with your camera looking at an empty space because the object you wanted it to see just happened to move to the side at the wrong time.

Race conditions suck, though, so LateUpdate() is there to let you choose what need to happen last.

avatar image
1

Answer by Doireth · Mar 30, 2016 at 09:09 AM

When you take one vector from another what you get is a directional vector from that vector to the other. For example

 offset = transform.position - player.transform.position;

Means that "offset" is a directional vector from "player.transform.position" to "transform.position". Keep in the mind that this is a direction and can be used anywhere in space (not dependent on starting point).

So, by using the code:

 transform.position = player.transform.position + offset;

You are taking the player's position and adding that direction (offset variable) to get the new position for the transform (transform.position).

You cannot "simplify" the code the way that you tried. It doesn't work that way.

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 neo1998x · Mar 30, 2016 at 02:48 PM 0
Share

Thank you :P

avatar image
1

Answer by giorashc · Mar 30, 2016 at 09:45 AM

When offset is first calculated in Start() the player is in a certain position. In the LateUpdate() method the player might already moved so its position is different then the one used in the Start() method so player.transform.position in the LateUpdate() method is not the same as it was in the Start() method.

The offset is used to move the camera so it will always be at the same distance and direction from the player.

(if the player does not move at all then you are right but most likely the player will move and change its position in the game)

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 neo1998x · Mar 30, 2016 at 02:48 PM 0
Share

Thanks this answered the question too! :P

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

Move the objects Y? 0 Answers

How to make my snowball model to actually shoot like a projectile? 0 Answers

Are nested ifs different from &&? 1 Answer

Have Two variables in a if statment? 1 Answer

How to interact with closed doors 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