space shooter smooth movement between x
I'm working on space shooter game in which you move the character across 5 different lanes on x coordinate. Thats done by setting xlimit to 18 in x and moving character by 9 in x. Or should i just scarp this idea and use rigidbody and velocity? I'm new to unity and game development in general and i can't make the character move smoothly between the "lanes". If someone could help me figure this out and maybe improve my code i would be really thankfull!
{
public float xlimit = 18.0f;
public float steg = 9.0f;
public Transform target;
public float smooth = 0.3f;
private float yVelocity = 9.0f;
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Vector3 position = this.transform.position;
this.transform.position = position;
if (position.x > -xlimit)
{
position.x--; //How can i improve this budget code?
position.x--;
position.x--;
position.x--;
position.x--;
position.x--;
position.x--;
position.x--;
position.x--;
this.transform.position = position;
}
else
{
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Vector3 position = transform.position;
transform.position = position;
if (position.x < xlimit)
{
float nyaPosition = Mathf.SmoothDamp(transform.position.x, (target.position.x + steg), ref yVelocity, smooth);
transform.position = new Vector3(nyaPosition, transform.position.y, transform.position.z);
}
else
{
}
}
}
}
Answer by ElijahShadbolt · Nov 27, 2016 at 09:10 PM
Here's how I would do it. Keep an integer for which lane you are currently in, and move towards that lane in world space.
Lanes.cs
using UnityEngine;
public class Lanes : MonoBehaviour
{
public Transform originTransform;
public int lanes = 5;
public float laneSize = 3f; // in world units
public float laneChangeSpeed = 30f; // in world units per second
public float yVelocity = 9f;
private int currentLane;
void Awake()
{
// start in middle lane
currentLane = (lanes-1) / 2;
Vector3 origin = originTransform != null ? originTransform.position : Vector3.zero;
transform.position = origin + new Vector3(currentLane * laneSize, 0, 0);
}
void Update()
{
// move left 1 lane
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
currentLane = Mathf.Clamp(currentLane - 1, 0, lanes);
}
// move right 1 lane
if (Input.GetKeyDown(KeyCode.RightArrow))
{
currentLane = Mathf.Clamp(currentLane + 1, 0, lanes);
}
Vector3 origin = originTransform != null ? originTransform.position : Vector3.zero;
Vector3 position = transform.position;
// move towards current lane
position.x = Mathf.MoveTowards(position.x, origin.x + currentLane * laneSize, laneChangeSpeed * Time.deltaTime);
// move upwards
position.y += yVelocity * Time.deltaTime;
// apply change in position
transform.position = position;
}
}
...
public int zeroLane = 3;
void Awake()
{
// start in middle lane
currentLane = 0;
...
}
void Update()
{
...
currentLane = $$anonymous$$athf.Clamp(currentLane - 1, zeroLane-lanes, lanes-zeroLane); // +1 for right, -1 for left
...
}
$$anonymous$$ake sure the $$anonymous$$athf.Clamp code inside the if statements use currentLane - 1
for left and currentLane + 1
for right:
currentLane = $$anonymous$$athf.Clamp(currentLane + 1, zeroLane-lanes, lanes-zeroLane);
Answer by stadskush · Nov 28, 2016 at 10:49 AM
Thank you! That worked perfectly. The only problem i have is that the charcter won't go under zero in x, is there any way to fix it?