using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
public GameObject playerObj;
public float smoothTime = 0.3f;
Vector2 velocity = Vector2.zero;
public int yOffset;
void Update()
{
Vector2 targetPosition = playerObj.transform.TransformPoint(new Vector3(0, yOffset));
if (targetPosition.y < transform.position.y) return;
targetPosition = new Vector3(0, targetPosition.y);
transform.position = Vector2.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}
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:
new Vector2( transform.position.x, transform.position.y)
and see if that fixes it.
thank you for your response, I proceed like this but the problem still persist
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
public GameObject playerObj;
public float smoothTime = 0.3f;
Vector2 velocity = Vector2.zero;
public int yOffset;
void Update()
{
Vector2 targetPosition = playerObj.transform.TransformPoint(new Vector3(0, yOffset));
if (targetPosition.y < transform.position.y) return;
targetPosition = new Vector3(0, targetPosition.y);
transform.position = Vector2.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
transform.position = new Vector3(transform.position.x, transform.position.y, -10);
}
}
var smoothTime = 1.5f;
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
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.