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 Coetzer · Sep 25, 2015 at 12:44 PM · transformcamera-movementlerpmathfif statement

Smooth camera to target Y-height

Hay guys,

I'm having some troubles with my MathF statements, could someone pleaaaase help me out, still pretty new to C# and getting some errors I don't understand

My errors are:

  1. Assets/Scripts/CameraController.cs(51,43): error CS1502: The best overloaded method match for UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid arguments 2. Assets/Scripts/CameraController.cs(52,112): error CS1502: The best overloaded method match for UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments

  2. Assets/Scripts/CameraController.cs(51,43): error CS1620: Argument #3' is missing ref' modifier

  3. Assets/Scripts/CameraController.cs(52,112): error CS1503: Argument #2' cannot convert UnityEngine.Vector3' expression to type `float'

Thank you!

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     //what is the player
 
     public PlayerController thePlayer;
 
     //where is the player
 
     private Vector3 lastPlayerPosition;
 
     //camera needs to move
 
     private float distanceToMoveX;
 
     // Smooth towards the height of the target
     
     private float cameraTargetHeight;
     public float smoothTime;
     public float yVelocity;
 
     private Vector3 newHeight;
 
     void Start () {
 
         //what is the player
         thePlayer = FindObjectOfType<PlayerController> ();
 
         //Initialize
         lastPlayerPosition = thePlayer.transform.position;
 
     }
 
     void Update () {
 
         //equation for camera move
         //new position minus old position
 
         distanceToMoveX = thePlayer.transform.position.x - lastPlayerPosition.x;
 
         transform.position = new Vector3(transform.position.x + distanceToMoveX, transform.position.y, transform.position.z);
 
         //move camera Y postion
 
         yVelocity = Camera.main.velocity.y;
 
         if (thePlayer.transform.position.y >= 4) {
 
             newHeight = Mathf.SmoothDamp(transform.position.y, transform.position.y + cameraTargetHeight,yVelocity,smoothTime);
             transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);    
         
         }
 
         //find player position
 
         lastPlayerPosition = thePlayer.transform.position;
     
     }
 }



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 Glurth · Sep 25, 2015 at 03:35 PM

1&2: In your smooth damp function, your third parameter is the "current speed", which is stored in the yVelocity variable you have. The smoothdamp function will need to MODIFY this varaible, so it is passed as a Reference. This means you need to add REF right in front of the variable name, to pass a "reference" to it, as opposed to just it's value.

 newHeight = Mathf.SmoothDamp(transform.position.y, transform.position.y + cameraTargetHeight,ref yVelocity,smoothTime);

Edit: also- you are assigning the return value of this function to a Vector3 variable, not a float. Perhaps you meant to define newHeight as a float?

1 & 3: Your Vector 3 function needs to take three float values. but your newHeight value is Vector3.

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

Answer by Dizy · Sep 25, 2015 at 04:03 PM

It seems that you are trying to follow a character on a 2D game, or something like.

It's a long way to move a cam on Y toward a new target, you can use this if you want to try (not tested) :

 var step = speed * Time.deltaTime;
 Vector3 newPosition = new Vector3(transform.position.x, thePlayer.position.y, transform.position.z);
 transform.position = Vector3.MoveTowards(transform.position, newPosition, step);
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
avatar image
0

Answer by Coetzer · Sep 27, 2015 at 09:15 AM

Thanks so much guys!

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 Coetzer · Sep 27, 2015 at 09:34 AM 0
Share

$$anonymous$$y code was also a little rigid since I hard coded the Trigger height,

I made the changes suggested by Glurth and also duplicated for downward motion

SO basically the camera only adjusts when player goes above a certain height

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : $$anonymous$$onoBehaviour {
 
     //what is the player
 
     public PlayerController thePlayer;
 
     //where is the player
 
     private Vector3 lastPlayerPosition;
 
     //camera needs to move
 
     private float distanceTo$$anonymous$$oveX;
 
     // Smooth towards the height of the target
     
     private float cameraTargetHeight;
     public float smoothTime;
     public float triggerHeight;
     private float yVelocity;
 
     private float newHeight;
 
     void Start () {
 
         //what is the player
         thePlayer = FindObjectOfType<PlayerController> ();
 
         //Initialize
         lastPlayerPosition = thePlayer.transform.position;
 
     }
 
     void Update () {
 
         //equation for camera move
         //new position $$anonymous$$us old position
 
         distanceTo$$anonymous$$oveX = thePlayer.transform.position.x - lastPlayerPosition.x;
 
         transform.position = new Vector3(transform.position.x + distanceTo$$anonymous$$oveX, transform.position.y, transform.position.z);
 
         //move camera Y postion
 
         yVelocity = Camera.main.velocity.y;
 
         if (thePlayer.transform.position.y >= (transform.position.y)+triggerHeight) {
 
         newHeight = $$anonymous$$athf.SmoothDamp(transform.position.y, transform.position.y + triggerHeight, ref yVelocity,smoothTime);
         transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);    
         
         }
 
         else if (thePlayer.transform.position.y <= (transform.position.y)-triggerHeight) {
             
         newHeight = $$anonymous$$athf.SmoothDamp(transform.position.y, transform.position.y - triggerHeight, ref yVelocity,smoothTime);
         transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);    
             
         }
 
     //find player position
 
         lastPlayerPosition = thePlayer.transform.position;
     
     }
 }

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

30 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

Related Questions

Final position of the camera movement 0 Answers

Simple Advice needed - Lerp, SmoothDamp, SmoothStep...... etc. 2 Answers

Coroutine: delay transform rotation but start transform movement immediately 2 Answers

gameobject position lerp 0 Answers

How can you increase a float to certain number and stop, while it is being used. 2 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