• 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 Selzier · Oct 16, 2014 at 06:56 PM · cameravector3temporaryvarible

Get a var's value plus X without modifying?

My camera follows (looks at) a GameObject that needs to be at the Player's 0,0,0. However I want the camera to follow 5 units in front of this GameObject. No matter what I've tried, the camera always moved forward 5 units, saves it's location, so when I run the game, the camera flies away.

 void Update(){
     target.position.x += 5f;
 }

I basically want to call the line above, WITHOUT saving that value to the actual position.

 void Update(){
     Transform temp = target;
     temp.position = new Vector3( temp.position.x + 5f, temp.position.y, temp.position.z );
 }

But this does not work either. Any Ideas how I can simply add X to a Transform.Position without moving it, or somehow put that into a temporary Transform that is reset each frame? The point is I need to calculate an offset while keeping the "LookAt" GameObject at player's 0,0,0.

Thanks!

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
0
Best Answer

Answer by tanoshimi · Oct 16, 2014 at 07:01 PM

The top example doesn't work because you are incrementing and reassigning target.position each frame - that's what += does.

The bottom example doesn't work because you're updating temp, which is a reference to target.

Try this:

 Transform temp = target.position + new Vector3(5f, 0, 0);

(Assuming that your object is rotated such that x is forward. Forward is normally z axis)

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 Selzier · Oct 16, 2014 at 07:30 PM 0
Share

" + new Vector3(5f, 0, 0);"

That's what I needed to do- Thanks!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How do I program camera movement 2 Answers

Movement relative to Camera and XZ Plane 1 Answer

transform.forward doesn't correspond to rotation 1 Answer

Cannot modify a value type return value of `UnityEngine.Transform.position'. Can anyone help ? 1 Answer

I want to create an interactive right analog MouseLook? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges