• 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 CleYTeX · Jan 11, 2019 at 11:58 AM · cameraoffset

Adding variable offset to camera

Hi,

i have a game where you have to move the player left and right to avoid hitting obstacles. The player and the camera get moved in the same way so that it looks like the player is a child of the camera. So there is a offset between the player and the camera. Now i want that the offset becomes smaller when the player hits obstacles. How can i do this with my code?

The player gets moved by this class:

 using UnityEngine;
 
 public class Move : MonoBehaviour {
 
     [Range(1, 2000)]
     public float speed;
 
     [Range(1, 2000)]
     public static float speedFactor;
 
     private float ScreenWidth;
 
     //Distance between the player and the camera
     public static Vector3 offset;
 
     // Use this for initialization
     void Start() {
         ScreenWidth = Screen.width;
         speed = 10f;
         speedFactor = 1.2f;
     }
     // Update is called once per frame
     void Update() {   
         move();
         moveMP();
     }
   void move() {

         gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime;
 
         //On PC
         if (Input.GetKey(KeyCode.A)) {
             gameObject.transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.S)) {
             gameObject.transform.position += Vector3.back * speed * Time.deltaTime;
         }


and the camera gets moved in the same way by this class:

 using UnityEngine;
 
 public class MoveCamera : MonoBehaviour
 {
     [Range(1, 2000)]
     public float speed;
 
     public static float speedFactor = 1.2f;
 
     public int ScreenWidth;
 
     // Use this for initialization
     void Start()
     {
         ScreenWidth = Screen.width;
         speed = 10f;
         speedFactor = 1.2f;
     }
     // Update is called once per frame
     void Update()
     {
         move();
         moveMP();
     }
     private void LateUpdate()
     {
         
     }
     void move()
     {
         gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime;
 
         //On PC
         if (Input.GetKey(KeyCode.A))
 
         {
             gameObject.transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.S))
         {
             gameObject.transform.position += Vector3.back * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.D))
 
         {
             gameObject.transform.position += Vector3.right * speed * Time.deltaTime;
 
         }
     }

How can i add the offset to this line:

 gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime;


This is of course not working:

 gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime + Hit.offset;

Hit.offset is of the type Vector3

When i have to this in an other way please tell me.

Thank you

Comment
Add comment · Show 1
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 esillen · Jan 11, 2019 at 12:41 PM 0
Share

It would help if you attached some kind of image that explains what you're trying to achieve.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CleYTeX · Jan 11, 2019 at 07:03 PM

The red cube is the player and consist of 125 smaller cubes. So i cant use the player as target because the origin of the player is changing every time he hits an obstacle.

alt text

After hitting obstacles the camera gets closer alt text

I already had a gameobject (cameraPlayer) which got moved by the MovingCamera script. The camera was following the cameraplayer:

   gameObject.transform.position = CameraPlayer.transform.position + Hit.offset;

The problem was that this had lead to an asymmetry between the player and the camera, because the player was already at the next position but the camera wasn't. So it looked like the player (the red cube) were shaking. So your solution is not working for me @WhiteCry but thank you anyway :)


playground-screenshot-20190111-19522184.png (161.1 kB)
playground-screenshot-20190111-19522680.png (108.9 kB)
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 RadonRaph · Jan 11, 2019 at 09:15 PM 0
Share

Okay so i've you try to add after gameObject.transform.position += Vector3.left * speed * Time.deltaTime;

 gameObject.transform.position -= offset;

maybe it will work ?

avatar image
0

Answer by RadonRaph · Jan 11, 2019 at 02:31 PM

Hello, in the update of the camera do this:

 transform.position = player.position + offset;

with this you only move the player with the input and the position of the camera is set from the player's position. Raph

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

158 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 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 avatar image avatar image avatar image avatar image

Related Questions

Offsetting local positions 2 Answers

Look to the side-Scripting Problem 2 Answers

Can I keep a player on the left side of the screen in relation to view resolution? 0 Answers

Camera zoom while keeping an off-center object at the same screen co-ordinates 1 Answer

How do you change the values of Vector3 Offset (x, y, z) in script randomly/overtime? 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