#Lerp or Lagging Constraint

1 messages · Page 1 of 1 (latest)

pure solar
#

i'm having trouble getting my head around this right now

#

how would i go about updating the position of the light and the player every frame?

#

and in what format?

sharp rose
#

are you using transform.position to move your player?

pure solar
#

the player movement is just rb.AddForce whenever the player inputs something

#

rb being Rigidbody ofc

sharp rose
#

ok so then I think that you can put lerp in your update method to make it move the light each frame

pure solar
#

in the script for controlling the ball i presume?

#

ill try putting something together and see how it works

sharp rose
#

actually the lerp should be in a script on the point light

#

I'm not sure if this will help much, but this is kinda what I would do

void Update()
{
    transform.position = Vector3.Lerp(transform.position, player.transform.position, 0.5f);
}
#

ignore discords formatting lol

pure solar
#

hmmm

#

just that alone seems to make the light follow the player exactly

#

and at the same point as the player

sharp rose
#

mabye try increasing the 0.5f, I just used that as an example, but it could probably be quite a bit bigger

pure solar
#

no change

#
{
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector3.Lerp(transform.position, player.transform.position, 5f);
    }
}
#

current script for the light, just in case ive made some obvious mistake

sharp rose
#

hmm, I'm not really sure, just trying something random here but mabye the number needs to be small

pure solar
#

there's no additional constraints or anything on the light

#

okay, ill give it a go

sharp rose
#

sry it's been a really long time since I've used that

pure solar
#

np, that still didnt seem to work though

#

in the documentation it seems to require an "interpolation ratio"

#

but i guess it isnt really important since you can just input any float so i dunno

sharp rose
#

hang on, I have an object with that function, I'm just gonna mess around with it a bit and see if I can figure something out

pure solar
#

alright, much appreciated

#

worst case scenario i can just use a parent constraint with lower weight and move the light to the middle of the level or something

#

will have a somewhat similar effect albeit not as cool

sharp rose
#

wierd, for me, I put it to a really small number like .05f and it seems to be working

pure solar
#

that's what i currently have it at

sharp rose
#

also I don't know if it makes a difference, I tried them both and they both seem to work for me, but I noticed that I was originally using Slerp not Lerp

pure solar
#

ah

#

well

#

ill give it a try?

#

no difference :(

#

are you sure that's all you did? is there nothing in the script that you control your ball with?

sharp rose
#

sry, I really just don't understand why it's not working

pure solar
#

maybe im calling the position of the player wrongly ?

#

:( thanks a lot for your help anyways

sharp rose
#

I notice that for player you have a gameobject and I just have the transform but that shouldn't be making a difference

pure solar
#

hm

#

changing it to a transform makes it not follow the player at all lol

sharp rose
#

oh nvm

pure solar
#

wait

#

actually

#

maybe it is working

#

wait nevermind, for some reason it slowed the ball down?

#

i.e the player

sharp rose
#

oh that's wierd

pure solar
#

but that means it's having some effect

#

ill try switching the transforms around

#

nope, still slowing down the ball

sharp rose
#

is there mabye a rigidbody on the light?

pure solar
#

nope

sharp rose
#

ok

#

is the light a child of the ball?

pure solar
#

nope, it's on it's own

sharp rose
#

nvm

pure solar
#

shame, i feel like it's close but i just dunno how it works

sharp rose
#

yeah I wish I understood it a bit better

#

I doubt this is affecting anything, but kinda the only other thing I'm noticing is that mine is in LateUpdate() instead of Update()

pure solar
#

im surprised that that didnt work

sharp rose
#

I guess there are a few other differences that I didn't mention because mine is for a camera, but I'll just post all my code

#
    public Transform followObject;
    public Transform lookAtObject;

    public float smoothFactor = 0.5f;

    public bool lookAtTarget;
    
    // Update is called once per frame
    void Update () {
        transform.position = Vector3.Lerp(transform.position, followObject.position, smoothFactor);
        transform.rotation = followObject.transform.rotation;

        if (lookAtTarget)
        {
            transform.LookAt(lookAtObject);
        }
    }```
pure solar
#

wow yea it really is that straightforward

#

i wonder what ive done wrong

#
    {
        Invoke("Lag", 0.1f);
    }
    void Lag()
    {
        transform.position = Vector3.Slerp(transform.position, player.transform.position, 0.5f);
    }```
sharp rose
#

it just doesn't make any sense to me because I can't figure out what's different

pure solar
#

i tried this but it didnt work either

sharp rose
#

sry what does Invoke do?

pure solar
#

it just calls whatever function is in quotes after a certain number of seconds

#

Invoke("<function>", float)

#

as far as i'm aware at least

sharp rose
#

ah ok that makes sense

pure solar
#

honestly even more confused that this didnt work

#

once the game is started, the light takes a few frames to get constrained to the ball, but after that it stays with it

#

and i have no idea what's going on in the background to cause that behaviour

sharp rose
#

it almost sounds like it's accelerating at the start, and then its speed never goes back down

pure solar
#

yeah but the speed matches the ball after that

#

regardless of how fast it's moving

#

but void Lag() shouldnt be running every frame so that shouldnt be possible, right?

sharp rose
#

I honestly don't know any more lol

pure solar
#

yeah, gonna have to give up on this for now until inspiration strikes i guess

#

thanks for fussing over this with me

sharp rose
#

ok

#

it's so wierd, I tried to stick my function on a light and it is working just fine so I have no clue what's wrong

#

hang on, I don't know if it's possible to do this, but could you like film it and show so I can see what's happening, because I just copy pasted your code in and it's working

#

the one thing I'm wondering if it has anything to do with how the ball is moving

pure solar
#

sorry for seeing this so late

#

that is so strange

#

which bit of code did you copy exactly

#

ive made a few changes so i need to stick it back in

sharp rose
#
public class LightLag : MonoBehaviour
{
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector3.Lerp(transform.position, player.transform.position, .05f);
    }
}```
#

just the original code you posted only I changed that to .05f

pure solar
#

oops i had a video in the bg lol

sharp rose
#

sry did you record this, just making sure

pure solar
#

yeah, why?

sharp rose
#

oh I get what you mean that's what that voice is lol

#

I wasn't sure if it was you talking about what was happening in the game or not

pure solar
#

oh haha

sharp rose
#

if you went into scene view instead of game view, does it look any different?

pure solar
#

nope, light follows the ball all the same

sharp rose
#

hmm, it just so wierd

#

have you tried making the number even smaller?

pure solar
#

wait

#

that actually fixed it lol

sharp rose
#

really?

pure solar
#

yep, light lags a bit behind now

#

wow that is so strange

sharp rose
#

yeah! lol

pure solar
#

maybe our scales are just different? (even though all my stuff is just scaled to 1)

sharp rose
#

I guess that would make sense, I think my character is about 3 high

pure solar
#

hmm

#

well now i want do something else, which is position constrain the light to be slightly above the ball

#

and still have it be affected by this script

#

but constraining seems to take priority

#

right now the light stays perfectly at the ball but i want it to be above and behind (and a bit to the left)

sharp rose
#

if you look at how my other script works, you could use that

#

it set's an offset at the start by figuring out where it is relative to the player and then instead of moving directly to the player, it moves to the players position + the offset

pure solar
#

🤔

#

im not sure what you're doing in that script exactly

#

is LookAt a unity thing?

#

or something in your own script?

sharp rose
#

oh sry, you can ignore that part, that only applies to the camera

pure solar
#

oh i see

#

ohhh i see

#

so instead of following the ball it's following a constrained object

sharp rose
#

is a constrained object like a child object?

pure solar
#

yea

sharp rose
#

that would probably work to, I didn't realize that when I copied the code I left removed the offset part

#
public Transform followObject;

public Vector3 cameraOffset;
public float smoothFactor = 0.5f;

// Use this for initialization
void Start () {
    cameraOffset = transform.position - followObject.transform.position;
}
    
// Update is called once per frame
void LateUpdate () {
    Vector3 newposition = followObject.transform.position + cameraOffset;
    transform.position = Vector3.Slerp(transform.position, newposition, smoothFactor);
}```
#

their that includes what I meant by offset, but I think you could do a constrained object as well

pure solar
#

ok

#

fantastic

#

it's all working now

#

thank you very much!!

#

might also apply it to the camera to see how it looks

sharp rose
#

glad it worked!

pure solar
#

oh my god this is so much better

#

thanks an absolute ton

sharp rose
#

np, really glad you like it