I'm very new to Unity (it hasn't been a full week since I started). For my first project I have decided to create a small game for mobile. It's kind of a way to get warm with the engine and eventually do something bigger. Anyway, I'm trying to do the following (and I try my best to explain it as good as possible):
The game will be an infinite runner. It's a 2D side scroller. If the player touches and holds the screen, the game runs in slow motion for a short duration. So far this works.
Now I want to add a cone or circle that appears when holding the button/screen. This cone indicates the travel distance. The player can move his fingers up or down to determine where exactly the character will be teleported to (indicated by a line). If the player lifts the finger/button the character should be teleported to the point where the line and circle cross each other and every enemy in his path takes damage / dies. It doesn't matter if the Player get's teleported or dashes at a high speed.
Here is a YouTube video of Katana Zero which is approximately what I want to achieve (00:21 min): https://www.youtube.com/watch?v=33L04ZSzYvY
I've tried stuff like applying force, but the player just jumps in an arc and not really in a straight line. Also I couldn't set a maximum distance to travel.
Maybe I should also note, that the movement speed of the player increases over time.
If anyone has a nice source so learn Unity I would be happy to soak all the wisdom in haha. So far I'm just modifying scripts I find online and mixing them together. Therefore it's really hard to do this.
This is my script, which also includes the slow motion trigger. So far I've been obviously testing this with keyboard buttons, in this case Jump / space.
public TimeManager timeManager;
[SerializeField] private float force = 5f; //Force that is implied to move the character forward
void Update()
{
if (Input.GetButtonDown("Jump"))
{
timeManager.DoSlowmotion();
Rigidbody2D rb = GetComponent<Rigidbody2D>();
// Gets the position of the mouse cursor in world space
Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rb.AddForce(direction * force, ForceMode2D.Impulse);
}
}
}
I also tried this:
public class Mouse : MonoBehaviour { public GameObject MousePosGameObject;
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = MousePosGameObject.transform.position.z - Camera.main.transform.position.z ;
MousePosGameObject.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}
}
It resulted in the Player just following the mouse without ever stopping.