Lerp | Linear interpolation microlibrary | iOS library
kandi X-RAY | Lerp Summary
kandi X-RAY | Lerp Summary
Lerp is a linear interpolation microlibrary. Lerp implements a Lerpable protocol on Float, Double, CGFloat, and CGPoint - mostly cribbed from this Stack Overflow post.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Lerp
Lerp Key Features
Lerp Examples and Code Snippets
Community Discussions
Trending Discussions on Lerp
QUESTION
I have an issue with jittery movement and I have searched the internet thin and tried countless solutions, but none have worked.
Essentially, I am moving a 2D Enemy GameObject towards my player, which involves moving and rotating at the same time.
At the start it is smooth, but when my player shoots the Enemy, causing it to fly backwards because of the RigidBody2D physics, it starts jittering when it rotates back towards my player.
Also, when my enemy tries to rotate back towards my player after getting hit, it struggles to aim/rotate directly at my player. It's just kind of struggling to rotate the last 20 degrees while jittering.
I have tried EVERY combination of using velocity and AddForce for movement, and using FromToRotation, RotateTowards, Lerp, and Slerp for rotation.
I have tried using both Update, LateUpdate, and FixedUpdate for either or both moving and rotating.
I have tried setting the GameObjects Interpolation to None, Interpolate and Extrapolate.
Nothing works.
My best guess is that my RotateEnemy() somehow gets confused about what "forward" is after getting hit, and doesn't know what to point at the player.
Here is a video showing the issue:
https://www.youtube.com/watch?v=SJwn4I74znQ&ab_channel=DanielNielsen
Here is the script I have on my Enemy gameobject:
...ANSWER
Answered 2022-Apr-11 at 11:13Based on your reply I would suggest you to call your RotateEnemy()
in Update
.
Update
runs on every frame, where FixedUpdate
does not - it runs per physics tick, and more or less than one of those may occur each frame.
And since we are not handling physics related stuff in RotateEnemy()
we should call it in Update()
QUESTION
I'm very new in Unity and Stackowerflow. If i did something wrong, please don't judge me ^^ I used Unity's TPS Controller asset for my game. In first, it worked very well. But then It broke. But i didn't do anything :( (i don't even touch scripts or prefabs). After that, i deleted asset and re-download it but it didnt work again. Here is one example from my broken scene and these are the codes from my controller. Thanks For Any Kind of Help.
Starter Assets Input ...ANSWER
Answered 2022-Apr-08 at 23:22I had the same problem too. I researched a lot of documents about that and finally, I solved this problem. The problem is not about your codes or events or smth else. The problem is related to Unity. I don't know the exact reason for the problem but you can solve it this way: First, go Edit > Project Settings and select Input System Package from the Left tab. And then, change the Update Method with Process Events In Dynamic Update. And that's all! Dynamic update means the usual Update method that you see in the scripts void Update().
ImagesQUESTION
I'm trying to use Matter.Query.region
to see if the character in my game is grounded. But, when I try to run region
with a bounds object that I created (displayed with the dots shown in the game), it doesn't come up with any collisions even though it is clearly intersecting other bodies.
Code:
...ANSWER
Answered 2022-Mar-24 at 00:20The bounds object doesn't appear to be properly created. The purple p5 vertices you're rendering may be giving you a false sense of confidence, since those aren't necessarily related to what MJS sees.
It's actually a pretty simple fix, passing an array of vertices instead of individual arguments:
QUESTION
I'm trying to make an Infinite marquee that speeds up on scroll, https://altsdigital.com/ you can see the effect on this website, the text says "Not your usual SEO agency" and when you scroll it speeds up.
Here's what I've tried but it does not work. It does not loop properly without overlapping (keep your eye on the left side of the page, you'll notice the text briefly overlaps and then translates left to create a gap) and I am unsure on how to fix it:
Here's the code (TEXT ONLY VISIBLE ON "FULL PAGE" view):
...ANSWER
Answered 2022-Feb-18 at 09:27Your items are overlapping because you're not allowing any lerping diffing when the items should switch positions.
The current
value should never equal the target
value. If the values match, than the current value needs to catch up the target
— giving that erratic movement and wrong calculations, additionally aggravated for the two sibling elements which should be perfectly in sync to give that immediate snap-back, perceived as a fluid continuous motion.
- Instead of animating two (or more) children independently,
animate only the parent.loop-container
. - The container should be as wide as one child element exactly.
- "Push" one child element to the far left using
position: absolute; left: -100%
- To allow the
target
value to be always greater than thecurrent
value:
when thetarget
value is greater than100
— setcurrent
to the negative difference of the two values, andtarget
to0
Demo time:
QUESTION
I'm trying to have an object oscillate between -45 and 45 degrees, and the following is the code I am using. Note that direction
is initialized to 1, zRotation
to 360, and speed
to 100. This works perfectly fine. However, when I change speed
to 1000, the object does a complete 360 rotation before continuing to oscillate correctly. I'm confused why that happens.
This is my code(inside the Update
method):
ANSWER
Answered 2022-Feb-20 at 14:22The problem is that you do not limit the zRotation
values between your desired values. In the current state of your code you only rely on your computer being fast and having little Time.deltaTime
values (the time elapsed since the last frame).
If you have any kind of hiccup (which happens often in the first couple of frames after starting up), the Time.deltaTime
value is going to be relatively large, resulting in a zRotation
that is so large that it takes longer to return into the [-45...45] range. You can check this by adding a line of
Debug.Log($"Time: {Time.deltaTime} - Rotation: {zRotation}");
to your update method.
This way the following scenario can occur (each line is the log of a frame):
- Time: 0 - Rotation: 360
- Time: 0,02 - Rotation: 380
- Time: 0,02 - Rotation: 420
- Time: 0,333 - Rotation: 733,33
In the last step there was a deltaTime
value that was so big that caused the zRotation
to get loose and go really far from your desired range.
If you limit the rotation values with
zRotation = Mathf.Min(Mathf.Max(zRotation, 315), 405);
You would not go out of the desired range. (Of course you have to update your if statements from <
to <=
or you could leave some kind of threshold in the Min(Max())
part.
I would also advise you use the modulo operator (%
) and initialize the value of zRotation
to 0
so that you don't have to constantly keep track of the 360-minAngle, 360+minAngle values, you could just use the angles in a much easier way and the modulo operator helps you stay in the [-360...360] range.
I updated your code:
QUESTION
First of all I want to apologise for my English cause I come from Poland and I'm still learning. I'm a beginner C#/Unity programmer and I have a pretty stupid/noob question/issue where player jumps twice when mashing the space. When the framerate is low for ex. 30, the problem occurs almost everytime and when framerate is for ex. 144 - hardly ever. I did some research and tried different methods. Firstly I checked whether I have all my inputs in Update and not FixedUpdate. That wasn't the problem at all. Then I tried replacing Input.GetKey to Input.GetKeyDown and GetKeyUp in Update as to ensure I have my boolean _spacePressed checked or not. That wasn't the solution too. The third thing I tried was to use Raycasting to check whether the player is grounded. With that I also checked whether when I jump, ray doesn't get checked twice. To make things clear I'm currently trying to make a 2.5D platformer. So to sum up I'm asking what could be the main issue (with input probably) where player jumps twice or even three times in a single frame when mashing space. Here's also my prototype code. I used enums to make a simple state "machine". If you have also any advice for me and my code to make things better apart from my question I would love to hear them. Thank you in advance!
...ANSWER
Answered 2022-Feb-15 at 23:28Keep in mind that FixedUpdate()
can happen a few times within a single frame. Check if _spacePressed == true
in the beginning of your Process_Jumping()
.
QUESTION
I'm trying to make my own moving solution for unity. Here's the code:
...ANSWER
Answered 2021-Sep-18 at 05:39UHM I fixed it... in the weirdest possible way? I'm not sure at all what caused the issue, but if anybody knows it, please share. Some people could find it interesting.
Anyways, here is the fix:
QUESTION
I'm trying to animate the movement of a sphere along a predefined set of vertices. I'm able to animate from one point to another using the below code
...ANSWER
Answered 2022-Feb-09 at 09:58You can animate a mesh along a path with plain three.js
by defining a curve based on your sequence of points and then using this curve for animation.
QUESTION
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fading : MonoBehaviour
{
[Header("Fading")]
public List objectsToFade = new List();
public float duration;
public Coroutine fadeCoroutine;
public bool automatic = false;
public bool startFading = false;
[Header("Random")]
public bool randomObjects = false;
public bool randomDuration = false;
public bool faded = false;
private bool fadeInOut = false;
private bool coroutineIsRunning = false;
private List objectsToFadeMaterials = new List();
private bool randomOnce = false;
private Material randomMaterial;
private float originalDuration;
private void Start()
{
originalDuration = duration;
for (int i = 0; i < objectsToFade.Count; i++)
{
objectsToFadeMaterials.Add(objectsToFade[i].GetComponent().material);
}
}
private void Update()
{
if (startFading)
{
if (automatic)
{
if (!coroutineIsRunning)
{
Fade();
}
}
else
{
if (Input.GetKeyDown(KeyCode.G))
{
Fade();
}
}
}
}
private void Fade()
{
fadeInOut = !fadeInOut;
if (fadeCoroutine != null)
StopCoroutine(fadeCoroutine);
if(randomDuration)
{
duration = Random.Range(1, 20);
}
else
{
duration = originalDuration;
}
if (randomObjects && objectsToFade.Count > 1)
{
if (randomOnce == false)
{
randomMaterial = objectsToFadeMaterials[Random.Range(0, objectsToFadeMaterials.Count)];
randomOnce = true;
}
if (fadeInOut)
{
fadeCoroutine = StartCoroutine(FadeTo(randomMaterial, 0, duration));
}
else
{
fadeCoroutine = StartCoroutine(FadeTo(randomMaterial, 1, duration));
}
}
else
{
for (int i = 0; i < objectsToFadeMaterials.Count; i++)
{
if (fadeInOut)
{
fadeCoroutine = StartCoroutine(FadeTo(objectsToFadeMaterials[i], 0, duration));
}
else
{
fadeCoroutine = StartCoroutine(FadeTo(objectsToFadeMaterials[i], 1, duration));
}
}
}
}
public IEnumerator FadeTo(Material material, float targetOpacity, float duration)
{
Color color = material.color;
float startOpacity = color.a;
float t = 0;
coroutineIsRunning = true;
while (t < duration)
{
t += Time.deltaTime;
float blend = Mathf.Clamp01(t / duration);
color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);
material.color = color;
if(t > duration)
{
coroutineIsRunning = false;
}
if(color.a == 1)
{
randomOnce = false;
}
if(color.a == 0)
{
faded = true;
}
yield return null;
}
}
}
...ANSWER
Answered 2022-Jan-09 at 22:53There are a lot of ways you could do this, but I personally in this case would create new coroutines FadeIn
and FadeOut
, instead of directly calling the following:
fadeCoroutine = StartCoroutine(FadeTo(objectsToFadeMaterials[i], 0, duration));
Then, at the end of your FadeOut
coroutine you can take some additional step(s) to trigger a teleport or whatever else you need to trigger. It looks like you don't want your Fading
to hold a reference to your Teleporting
, which is smart, so you could choose to fire an event instead that your Teleporting
component can subscribe to.
QUESTION
how are you? i'm learning flutter and i'm bloqued by flutter excetion and i don't know how resolve it.I want to make a progressBar for a quizz gma. i would like to make a 60 secondes progress bar but When i'm execute my code i get this error:
...ANSWER
Answered 2021-Sep-20 at 13:38change Tween(begin: 0, end: 1)
to Tween(begin: 0.0, end: 1.0)
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Lerp
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page