Search Unity

please can you help solving this, i have a problem with SmoothDamp

Discussion in 'Scripting' started by abdelyo, Oct 1, 2018.

  1. abdelyo

    abdelyo

    Joined:
    Sep 13, 2018
    Posts:
    2
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class FollowPlayer : MonoBehaviour {
    7.  
    8.     public GameObject playerObj;
    9.     public float smoothTime = 0.3f;
    10.     Vector2 velocity = Vector2.zero;
    11.     public int yOffset;
    12.  
    13.     void Update()
    14.     {
    15.         Vector2 targetPosition = playerObj.transform.TransformPoint(new Vector3(0, yOffset));
    16.         if (targetPosition.y < transform.position.y) return;
    17.  
    18.         targetPosition = new Vector3(0, targetPosition.y);
    19.  
    20.         transform.position = Vector2.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    21.     }
    22. }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It's probably because transform.position is a Vector3, not a Vector2, but I'm just guessing.

    You can make a Vector2 out of transform.position by replacing it with:

    Code (csharp):
    1. new Vector2( transform.position.x, transform.position.y)
    and see if that fixes it.
     
  3. abdelyo

    abdelyo

    Joined:
    Sep 13, 2018
    Posts:
    2
    thank you for your response, I proceed like this but the problem still persist
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowPlayer : MonoBehaviour {
    6.  
    7.     public GameObject playerObj;
    8.     public float smoothTime = 0.3f;
    9.     Vector2 velocity = Vector2.zero;
    10.     public int yOffset;
    11.  
    12.     void Update()
    13.     {
    14.         Vector2 targetPosition = playerObj.transform.TransformPoint(new Vector3(0, yOffset));
    15.         if (targetPosition.y < transform.position.y) return;
    16.  
    17.         targetPosition = new Vector3(0, targetPosition.y);
    18.         transform.position = Vector2.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    19.         transform.position = new Vector3(transform.position.x, transform.position.y, -10);
    20.  
    21.        
    22.     }
    23. }
     
  4. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    Code (CSharp):
    1. var smoothTime  = 1.5f;
    2. transform.position = new Vector3(transform.position.x, transform.position.y, -10 , smoothTime);
    Try add smooth time and see it is different. if not try use Vector2.Lerp instead
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Let me clarify my post.

    From your FIRST listing, on line 20, replace "transform.position" with the blob of code in my post above.

    Do not type Vector3... use copy/paste because I wrote Vector2 if you look closely, and your second post has it copied as Vector3. It matters.