Android touch camera control with inertia in Unity3d - c#

I'm working on a script for Android tablet devices in Unity3d, where the user drags to move the camera. I want the "ground" at the touch position to stay underneath the users finger while he is panning. Here is my simplyfied working code so far:
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
Plane plane = new Plane(Vector3.forward, Vector3.zero);
Vector2 worldStartPoint;
void Update()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
this.worldStartPoint = GetWorldPoint(touch.position);
}
if (touch.phase == TouchPhase.Moved)
{
Vector2 worldDelta = GetWorldPoint(touch.position) - worldStartPoint;
transform.Translate(-worldDelta.x, -worldDelta.y, 0);
}
}
}
Vector2 GetWorldPoint(Vector2 screenPoint)
{
Ray ray = Camera.main.ScreenPointToRay(screenPoint);
float rayDistance;
if (plane.Raycast(ray, out rayDistance))
return ray.GetPoint(rayDistance);
return Vector2.zero;
}
}
Now the problematic part: I would like the camera to move like a physics object once the user lifts up his finger. I'm trying to calculate the current velocity while dragging and then applying it as a dampened/inertia-like effect while not currently dragging. Theoretically I would do this:
Vector2 worldStartPoint;
Vector3 velocity;
void Update()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
this.worldStartPoint = GetWorldPoint(touch.position);
}
if (touch.phase == TouchPhase.Moved)
{
Vector2 worldDelta = GetWorldPoint(touch.position) - worldStartPoint;
transform.Translate(-worldDelta.x, -worldDelta.y, 0);
velocity = worldDelta / Time.deltaTime;
}
}
else
{
transform.Translate(-velocity * Time.deltaTime);
velocity = Vector3.MoveTowards(velocity, Vector3.zero, damping * Time.deltaTime);
}
}
So, I always calculate velocity while moving, and once I stop input, it should remain at the last known velocity, apply it and reduce from there on until stop. However, usually the last velocity is zero, because it appears that when dragging/swiping across the screen the finger actually stops shortly and reports zero velocity before TouchPhase.Moved is over.
Now my solution/workaround would be to keep an array of velocities of the last few frames (maybe 30) and once the finger is lifted up, I would calculate the average velocity.
Vector3[] velocityBuffer = new Vector3[bufferSize];
int nextBufferIndex;
Vector3 GetAverage()
{
Vector3 sum = Vector3.zero;
for (int i = 0; i < bufferSize; i++)
sum += velocityBuffer[i];
return sum / bufferSize;
}
This works a little better, since more often than not it reports at least some velocity, but in total it's not much better and also feels very hacky. Depending on the speed of the touch, I might end up with 20 entries of zero velocity, making the damping much too strong, and sometimes the velocity randomly becomes so big that the camera just flicks a few hundred units.
Is there anything wrong with my calculations or am I overseeing something simple to fix this? Does anybody have a working solution of a smooth camera drag? I looked at a few mobile games and a lot of them actually were feeling a little clumsy, like snapping the camera to the finger position, after a few pixels of deltaMovement and then suddenly releasing it without any damping.

I'm not feeling as if I've found the perfect solution, but at least by now I have something which works better and also is more "physically correct". First, I am now storing the camera position and deltaTime in my buffer instead of the velocity. This way I can calculate a rolling average every 10 frames with the correct time factor.
/// <summary>
/// Stores Vector3 samples such as velocity or position and returns the average.
/// </summary>
[Serializable]
public class Vector3Buffer
{
public readonly int size;
Sample[] sampleData;
int nextIndex;
public Vector3Buffer(int size)
{
if (size < minSize)
{
size = minSize;
Debug.LogWarning("Sample count must be at least one. Using default.");
}
this.size = size;
sampleData = new Sample[size];
}
public void AddSample(Vector3 position, float deltaTime)
{
sampleData[nextIndex] = new Sample(position, deltaTime);
nextIndex = ++nextIndex % size;
}
public void Clear()
{
for (int i = 0; i < size; i++)
sampleData[i] = new Sample();
}
public Vector3 GetAverageVelocity(Vector3 currentPosition, float currentDeltaTime)
{
// The recorded sample furthest back in time.
Sample previous = sampleData[nextIndex % size];
Vector3 positionDelta = currentPosition - previous.position;
float totalTime = currentDeltaTime;
for (int i = 0; i < size; i++)
totalTime += sampleData[i].deltaTime;
return positionDelta / totalTime;
}
[Serializable]
struct Sample
{
public Vector3 position;
public float deltaTime;
public Sample(Vector3 position, float deltaTime)
{
this.position = position;
this.deltaTime = deltaTime;
}
}
public const int minSize = 1;
}
Also, I noticed, that I was recording a lot of zero velocity values, which is now mitigated because I'm tracking the position, which I also want to update when not dragging, but holding:
if (input.phase == TouchPhase.Moved || input.phase == TouchPhase.Stationary)
{
velocityBuffer.AddSample(transform.position, Time.deltaTime);
}
if (input.phase == TouchPhase.Ended || input.phase == TouchPhase.Canceled)
{
velocity = -velocityBuffer.GetAverageVelocity(transform.position, Time.deltaTime);
}
Lastly, instead of setting the camera to the fingers world position every frame, I use a tiny bit of Lerp/MoveTowards interpolation to smooth out any jitter. It's hard to get best value between crisp control and smooth look, but I assume that's the way to go with user input, which can vary rapidly.
Of course, I'd still be interested in other approaches, better solutions or opinions about my current implementation.

Related

Swerve Control Unity C#

Hi I am creating a hyper casual game with unity, but I have encountered a problem with the swerve control (I have also seen many git hubs but even these do not work perfectly)
I've put this in my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float lastframeposx;
private float movefactorx;
public float MoveFactorX => movefactorx;
public Camera m_MainCam;
private float speed = 2.0f;
[SerializeField]
GameObject character;
[SerializeField] private float swerveSpeed = 0.5f;
[SerializeField] private float maxSwerveAmount = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position += Vector3.forward * speed * Time.deltaTime;
Cammina();
/*Vector3 destra = Camera.main.ScreenToWorldPointt(Input.touches[i].position);
transform.position += Vector3.zero destra;*/
}
void Cammina()
{
if(Input.GetMouseButtonDown(0))
{
lastframeposx = Input.mousePosition.x;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
else if (Input.GetMouseButton(0))
{
movefactorx = Input.mousePosition.x - lastframeposx;
lastframeposx = Input.mousePosition.x;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
else if(Input.GetMouseButtonUp(0))
{
movefactorx = 0f;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
}
/*void vaidoveschiacciato()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.collider != null)
{
transform.position += hit.collider.GetComponent<Transform.position> * speed * Time.deltaTime;
}
}
}
}*/
}
1 Problem: he don't go when the finger is
2 Problem: How do I eliminate the movement from right to left (Without making it go out of the path)
(Langauge: C#)
The problem: When you swerve, it swerves just in the direction, there is no limits on how far it goes.
How I would fix this: I would put the movement to change it through a function. This could clamp it, so the higher the distance to the center of the track, the less it swerves. Or, you can altogether check if the distance is a maximum and then stop swerving.
Note: you can use other functions to do this (they just have to flatten out the larger the input).
Smooth, good looking bell curve way
For example you could use a bell curve. Look one up if you've never seen one before. It is at it's highest possible output of one, at a zero input. When it gets hiher or lower, it outputs lower to zero.
the simplest equation is y = i-(x2). The lower i is (above 1), the wider the curve, or the larger the output is for a larger input. I made a graph here.
x can be the distance to the center of the track, so you should adjust i, so the maximum distance from the track is flat.
This output is what you should change swerveAmount to.
The flatter parts of the graoh is how much you will swerve when you are that distance from the center (x axis)
Alternatively
You could just check the distance, and if it passes a certain distance don't translate it.
Let me know in the omments if there are any problems! :)

Jitter when moving around a circular path

This is a pretty strange bug. I created the following script intended to calculate positions to shoot projectiles in my game by moving around a circular path and facing the mouse:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileFire : MonoBehaviour
{
[System.Serializable]
public class ButtonDirectionSet {
public string button;
public float direction;
public bool degrees = true;
}
public GameObject projectile;
public Vector2 centerPoint;
public float radius = 1.0f;
public string fireButton = "Fire1";
public bool followMouse = false;
public bool followKeyboard = true;
public List<ButtonDirectionSet> buttonDirectionMap = new List<ButtonDirectionSet>();
public bool degrees = true;
public float direction;
private readonly float maxDirection = 360 * Mathf.Deg2Rad;
// Start is called before the first frame update
void Start()
{
foreach (ButtonDirectionSet set in buttonDirectionMap) {
if (set.degrees) {
set.direction *= Mathf.Deg2Rad;
}
}
if (degrees) {
direction *= Mathf.Deg2Rad;
}
}
void Update()
{
foreach (ButtonDirectionSet set in buttonDirectionMap) {
if (Input.GetButtonDown(set.button)) {
direction = set.direction;
}
}
if ((followKeyboard && followMouse && Input.GetMouseButton(0)) || !followKeyboard) {
Vector3 mouse = Input.mousePosition;
mouse.z = transform.position.z - Camera.main.transform.position.z;
Vector3 mousePosition = transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(mouse));
float xPos = mousePosition.x - centerPoint.x;
if (Mathf.Abs(xPos) < 0.1f)
{
xPos = 0.1f * Mathf.Sign(xPos);
}
float yPos = mousePosition.y - centerPoint.y;
if (Mathf.Abs(yPos) < 0.1f)
{
yPos = 0.1f * Mathf.Sign(yPos);
}
direction = Mathf.Atan2(yPos, xPos);
}
if (direction < 0) direction += maxDirection;
transform.localPosition = centerPoint + (new Vector2(Mathf.Cos(direction), Mathf.Sin(direction)) * radius);
}
}
If you set both "follow keyboard" and "follow mouse" to true, the object should be positioned toward the mouse when you hold down the left click button. For the most part, this works, but for some reason, there's jitter in the sense that the object rapidly changes between two different positions for a period of time, even when I don't move the mouse at all. Additionally, the "direction" value switches itself at the same rate, even if I don't move my mouse at all. This does not happen all the time, but it does happen pretty frequently at times that appear to be random.
Is there anything I can do to mitigate or eliminate this behavior?
Since the direction also jitters, there is something wrong with the way you calculate the direction.
The reason your direction jitters is because of the transform.InverseTransformPoint. Since the position of the object changed, the position of the mouse relative to the object also changed with every movement so the direction also changed.
Here are two ways to do it:
1:
Vector2 direction;
if ((followKeyboard && followMouse && Input.GetMouseButton(0)) || !followKeyboard)
{
Vector3 mouseOnScreen = Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector3(centerPoint.x, centerPoint.y, 0);
direction = new Vector2(mouseOnScreen.x, mouseOnScreen.y);
}
transform.localPosition = centerPoint + (direction.normalized * radius);
This is a simple way to do it without using angles. It gets the position of the mouse on screen in world space. Then it makes it into a vector 2 so the z value doesn't affect it. Then, it sets the new position to the center plus the direction to the mouse with a magnitude of the radius
2:
if ((followKeyboard && followMouse && Input.GetMouseButton(0)) || !followKeyboard)
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector3(centerPoint.x, centerPoint.y, 0);
direction = Mathf.Atan2(mousePosition.y, mousePosition.x);
}
transform.localPosition = centerPoint + (new Vector2(Mathf.Cos(direction), Mathf.Sin(direction)) * radius);
This is the same way you did it but a little cleaned up. Instead of using transform.InverseTransformPoint, this way subtracts the centerPoint. That returns the vector from the centerPoint to the mouse, setting the centerPoint as the origin. This is also in the firsts solution.

How to realistically reflect a 3d sphere in Unity with C#

I've been trying to realistically reflect a 3d sphere on the walls of a box for a while now in Unity. For some reason, the reflection is generally correct, but when the ball hits a wall in certain directions, the reflection is incorrect.
To illustrate what happens to the ball upon hitting a wall: T = top wall, R = right wall, L = left wall, and B = bottom wall. Let r = the ball comes/goes to the right, l = for the left, and s = the ball stops/slows down significantly. The instructions below take this format: Xyz, where X = the wall the ball is about to hit, y = the ball's initial direction, z = the reflection. The game has a top-down perspective, and the instructions are based on the wall's perspective. I'm also new to C#, so the code is potentially eye burning.
Instructions: Tll, Trl; Bll, Brl; Rls or after hitting another wall Rlr, Rrl; Lls or after hitting another wall Llr, Lrl
Generally, when the ball stops, it jumps in the air. I wonder if this is because the angle reflects along the wrong axis, but why would this only sometimes happen? Also, when only one key is held, the ball bounces back and forth until it leaves the arena. I know about discrete and continuous hit detection, and the setting is on discrete, but the walls generally contain the ball well enough, with this case being the exception.
What I tried:
Figuring out how to use Vector3.Reflect. I do not understand what variables this function should contain and how to apply this to my code. I did look at the Unity Documentation page for help, but it did not answer my question.
Changing the negative signs, as the angle has to be a reflection on the y-axis, and this does change how the reflections work, but does not solve the problem. The current way the negatives are ordered are the most ideal I found.
Giving the ball a physics material for bounciness.
Adding a small number to the denominator in the arctan equation to help prevent a division by zero. This did not help at all.
Creating different equations (basically changing the negatives) for varying combinations of positive and negative accelerations. Since there is a certain positive or negative acceleration associated with each button press (see Movement script), and there seems to be an issue with said signs, I wondered if associating each acceleration with its own set of equations could solve the problem. It did not work.
Checked if the walls were at different angles.
Deleting the variables xA and yA, or placing them in different spots.
Experimented with finding the speed of the object, but had no idea how to implement it.
The code for the Movement script for the player named Controller:
public class Movement : MonoBehaviour
{
public static float xAcceleration = 0.0f;
public static float yAcceleration = 0.0f;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W)) //If the key W is pressed:
{
Vector3 position = this.transform.position; //Variable position is set to transform the players placement in the game.
if (yAcceleration >= -5 && yAcceleration <= 5) //If the y vector of the acceleration is >= -5 and <= 5:
{
yAcceleration = yAcceleration + 0.01f; //The y vector of the acceleration increases by 0.01 as long as the key W is pressed.
}
position.z = position.z + (0.1f * yAcceleration); //The position of the object on the z-axis (pretend it is the y-axis in the game world) is transformed by its original position plus its speed times its yAcceleration.
this.transform.position = position; //The gameObject is now transformed to a position equal to the variable position by the z-axis.
}
else //If the key W is let go of:
{
Vector3 position = this.transform.position;
position.z = position.z + (0.1f * yAcceleration);
this.transform.position = position; //The position of the gameObject continues to update, but its acceleration does not change. Basically, it continues to move forward.
}
//The rest of the code is very similar to the above, but I included it just in case there was something wrong.
if (Input.GetKey(KeyCode.S))
{
Vector3 position = this.transform.position;
if (yAcceleration >= -5 && yAcceleration <= 5)
{
yAcceleration = (yAcceleration) - 0.01f;
}
position.z = position.z + (0.1f * yAcceleration);
this.transform.position = position;
}
else
{
Vector3 position = this.transform.position;
position.z = position.z + (0.1f * yAcceleration);
this.transform.position = position;
}
if (Input.GetKey(KeyCode.A))
{
Vector3 position = this.transform.position;
if (xAcceleration >= -5 && xAcceleration <= 5)
{
xAcceleration = (xAcceleration) - 0.01f;
}
position.x = position.x + (0.1f * xAcceleration);
this.transform.position = position;
}
else
{
Vector3 position = this.transform.position;
position.x = position.x + (0.1f * xAcceleration);
this.transform.position = position;
}
if (Input.GetKey(KeyCode.D))
{
Vector3 position = this.transform.position;
if (xAcceleration >= -5 && xAcceleration <= 5)
{
xAcceleration = (xAcceleration) + 0.01f;
}
position.x = position.x + (0.1f * xAcceleration);
this.transform.position = position;
}
else
{
Vector3 position = this.transform.position;
position.x = position.x + (0.1f * xAcceleration);
this.transform.position = position;
}
}
}
This is the code for the collider and reflection:
public class Collider : MonoBehaviour
{
public float xA;
public float yA;
void OnCollisionEnter(Collision collision) //If a gameObject enters the collision of another object, this immediately happens once.
{
if (gameObject.tag == "Boundary") //If the gameObject has a tag named Boundary:
{
yA = -Movement.yAcceleration; //yA stores the value of yAcceleration after being called from script Movement as a negative. Its a reflection.
Movement.xAcceleration = (Movement.xAcceleration * -Mathf.Atan(yA / Movement.xAcceleration)); //xAcceleration is changed based on this equation: A * artan(A_y / A_x). The 0.000001 was here, adding to A_x to help prevent a 0 as the denominator.
xA = Movement.xAcceleration; //This is declared now...
Movement.yAcceleration = (Movement.yAcceleration * Mathf.Atan(Movement.yAcceleration / xA)); //This uses xA because Movement.xAcceleration is changed, and the yAcceleration calculation is based on the xAcceleration prior the collision.
}
}
void OnCollisionStay(Collision collision)
{
if (gameObject.tag == "Boundary")
{
yA = Movement.yAcceleration; //The same thing happens as before.
Movement.xAcceleration = (Movement.xAcceleration * -Mathf.Atan(yA / Movement.xAcceleration));
xA = Movement.xAcceleration;
Movement.yAcceleration = (Movement.yAcceleration * Mathf.Atan(Movement.yAcceleration / xA));
Movement.xAcceleration = -Movement.xAcceleration / 2; //On collision, the ball is reflected across the x-axis at half its speed.
Movement.yAcceleration = Movement.yAcceleration / 2; //yAcceleration is half its original value.
}
}
}
The picture below is the game setup. I apologize that it is a link; I do not have enough Reputation to merit a loaded image on this page. Also, if anything is unclear, please message me.
https://i.stack.imgur.com/VREV4.png
I would really appreciate the help. Thanks!
One very important note here: As soon as there is any Rigidbody involved you do not want to set any values through the .transform - This breaks the physics and collision detection!
Your Movement should rather alter the behavior of the Rigidbody e.g. by simply changing its Rigibody.velocity
I would then also place the collision check directly into the balls's component and check whether you hit a wall ("Boundary")
Then another note: Your code is currently frame-rate dependent. It means that if your target device runs with only 30 frames per second you will add 0.3 per second to the acceleration. If you run however on a more powerful device that manages to run with 200 frames per second then you add 2 per second.
You should rather define the de/increase per second and multiply it by Time.deltaTime
All together maybe something like this
public class Movement : MonoBehaviour
{
// Adjust these settings via the Inspector
[SerializeField] private float _maxMoveSpeed = 5f;
[SerializeField] private float _speedIncreasePerSecond = 1f;
// Already reference this via the Inspector
[SerializeField] private Rigidbody _rigidbody;
private void Awake()
{
if(!_rigidbody) _rigidbody = GetComponent<Rigidbody>();
}
// Get User Input in Update
private void Update()
{
var velocity = _rigidbody.velocity;
velocity.y = 0;
if (Input.GetKey(KeyCode.W) && velocity.z < _maxMoveSpeed)
{
velocity.z += _speedIncreasePerSecond * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S) && velocity.z > -_maxMoveSpeed)
{
velocity.z -= _speedIncreasePerSecond * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A) && velocity.x > -_maxMoveSpeed)
{
velocity.x -= _speedIncreasePerSecond * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D) && velocity.x < _maxMoveSpeed)
{
velocity.x += _speedIncreasePerSecond * Time.deltaTime;
}
// clamp to the max speed in case you move diagonal
if(velocity.magnitude > _maxMoveSpeed)
{
velocity = velocity.normalized * _maxMoveSpeed;
}
_rigidbody.velocity = velocity;
}
}
And then finally simply add a PhysicsMaterial with desired settings to the walls and ball.
I used Friction = 0f and Bounciness = 0.7f for ball and walls. For slow movements you also might want/have to adjust the Bounce Threshold in the Project's Physics Settings otherwise there will be no bouncing if the velocity is smaller then 2 by default.
This depends a bit on your definition of "realistic". I disabled gravity so the ball also has no rotation and angular friction:

How to change lane smoothly

I am trying to achieve basic racing game. Infinite racing game, movement method like a subway surfers. I have a problem about changing lane. I dont want to teleport to other lane, I want to smoothly. I am newbee in unity, I have try Lerp method but it is not working.
using UnityEngine;
using System.Collections;
public class VehicleController : MonoBehaviour
{
public float drift;
public Vector3 positionA;
public Vector3 positionB;
public Vector3 positionC;
public Vector3 positionD;
private Transform tf;
private Rigidbody rb;
private Vector3 vehiclePos;
void Awake()
{
//rb = GetComponent<Rigidbody> ();
tf = transform;
}
void Update()
{
vehiclePos = tf.position;
if (Input.GetKey( KeyCode.Space ))
DecreaseSpeed ();
else
IncreaseSpeed ();
if (Input.GetKeyDown (KeyCode.A))
{
MoveToRight ();
Debug.Log( "Move to Right!" );
}
if (Input.GetKeyDown (KeyCode.D))
{
MoveToLeft ();
Debug.Log( "Move to Left!" );
}
}
void FixedUpdate()
{
tf.Translate (Vector3.forward * speed * Time.deltaTime);//My Movement Method.
}
void MoveToLeft()
{
if (vehiclePos.position.x == positionA.x)
vehiclePos = Vector3.Lerp (vehiclePos.position, positionB, Time.deltaTime * drift);
}
void MoveToRight()
{
if (vehiclePos.position.x == positionB.x)
vehiclePos = Vector3.Lerp (vehiclePos.position, positionA, Time.deltaTime * drift);
}
}
First: Don't use == for position.x, since it's a floating-point (decimals) value and in this case it would be very rare for it to return "true". Here's some info about comparing floats.
Second: It doesn't look like you're connecting your actual position with vehiclePos anywhere. transform.position is what you want there.
Third: Input.GetAxis() is a cleaner way to deal with direction input. Instead of specifically calling out each button you can deal with just one float value between -1 and 1. It will also let you reconfigure the keys easily.
Fourth: In an infinite runner it is better to have the world move towards your character and camera than to have the character and camera actually move forward. Floating point numbers get less precise as you move further away from zero, so you should have your action take place relatively close to the world origin (0,0,0) point if you can.
If you want to press the button once to change lanes, you should keep an integer variable that saves which lane you're currently in. If you press LEFT you subtract one, and if you press RIGHT you add one. You should also add a check to make sure it stays within the desired range.
Then in Update() you just need to ALWAYS Lerp towards that X value. You can use Mathf.Lerp to lerp only one variable at a time if you want.
public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Update() {
//"Horizontal" is a default input axis set to arrow keys and A/D
//We want to check whether it is less than the deadZone instead of whether it's equal to zero
float input = Input.GetAxis("Horizontal");
if(Mathf.Abs(input) > deadZone) {
if(!didChangeLastFrame) {
didChangeLastFrame = true; //Prevent overshooting lanes
laneNumber += Mathf.roundToInt(Mathf.Sign(input));
if(laneNumber < 0) laneNumber = 0;
else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
} else {
didChangeLastFrame = false;
//The user hasn't pressed a direction this frame, so allow changing directions next frame.
}
Vector3 pos = transform.position;
pos.x = Mathf.Lerp(pos.x, firstLandXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
transform.position = pos;
}
You could likely just use this code as-is, but I suggest you look it over and try to figure out an understanding of how and why it works. A newbie today who always seeks to improve their skill can do something amazing next week. Hope this helps. :)

XNA make sprite follow the mouse pointer but with a delay?

Here I updated my code with your help.
Anyway it still does not do what is supposed to do, following the mouse pointer with a delay.
The balloon (the sprite) is flying diagonally and does not stop once the mouse pointer is met, only slows down then keeps moving and then speeds up.
I added an if condition once the balloonPosition is equal to mouse pointer, to have the velocity = 0, but that does not stop the balloon.
I added a portion of code for keeping the balloon (the sprite) in the screen.
protected override void Update(GameTime gameTime)
{
MouseState currentMouseState = Mouse.GetState();
//balloonPosition = new Vector2(currentMouseState.X, currentMouseState.Y);
//System.Windows.Input.MouseState currentMouseState = System.Windows.Input.Mouse.GetState();
// Get the current mouse position
Vector2 mousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
// Get the distance between the balloon and the mouse.
float distance = Vector2.Distance(mousePosition, balloonPosition);
// You can change the standard velocity / or the max distance to make the sprite move faster or slower.
// Currently it may move to fast or to slow for you to know a difference.
balloonVelocity = StandardVelocity * (distance/MaxDistance);
// Set the balloons position to the new velocity.
balloonPosition += balloonVelocity;
if (balloonPosition == mousePosition)
{
balloonVelocity = new Vector2(0);
}
//Keep the balloon in the screen
if (balloonPosition.X < balloon.Width / 2)
balloonPosition.X = balloon.Width / 2;
if (balloonPosition.Y < balloon.Height / 2)
balloonPosition.Y = balloon.Height / 2;
if (balloonPosition.X > Window.ClientBounds.Width - balloon.Width / 2)
balloonPosition.X = Window.ClientBounds.Width - balloon.Width / 2;
if (balloonPosition.Y > Window.ClientBounds.Height - balloon.Height / 2)
balloonPosition.Y = Window.ClientBounds.Height;
}
NewPosition = Vector2.Lerp(CurrentPosition, DesiredPosition, Speed);
Here's what I'd go with off the top of my head:
public List<Tuple<TimeSpan, Vector2>> Points { get; set; }
public Vector2 CurrentPoint { get; set; }
public void Update(GameTime gameTime)
{
Points.Add(new Tuple<TimeSpan, Vector2>(TimeSpan.FromSeconds(1), mouseCoords));
foreach(var point in Points)
{
point.Item1 -= gameTime.ElapsedTimeSpan;
if (point.Item1 < TimeSpan.Zero)point
CurrentPoint = point.Item2;
}
}
I'm pretty sure I wouldn't need to explain the code, right?
In the draw loop, you'd simply always draw the CurrentPoint
You can use a list or an array to keep the delay. Store the mouse position every 0.03125 seconds in a list. Then set timer for the delay (integer counter should work). When the delay is up start reading from the list at the same pace it was out in the list. (0.03125 in this case) and move your sprite towards that point.
Since you are looking for how to change the speed this is one way to do it.
Vector2 balloonOrigin, balloonPosition;
Vector2 balloonVelocity;
private float MaxDistance = 1920; // Reduce this value to slow down the balloon.
public static float AngleBetweenVectors(Vector2 v1, Vector2 v2) { return (float)Math.Atan2(v2.Y - v1.Y, v2.X - v1.X); }
public void test()
{
//System.Windows.Input.MouseState currentMouseState = System.Windows.Input.Mouse.GetState();
// Get the current mouse position
Vector2 mousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
// Get the distance between the balloon and the mouse.
float distance = Vector2.Distance(mousePosition, balloonPosition);
// You can change the max distance to make the sprite move faster or slower.
// Currently it may move to fast or to slow for you to know a difference.
balloonVelocity = AngleBetweenVectors(balloonPosition, mousePosition) * (distance / MaxDistance);
// Set the balloons position to the new velocity.
balloonPosition += balloonVelocity;
}

Categories