- Home /
Switching Splines on the go
Hi guys, still fairly new to programming (C#) and unity and was wondering if I could get some help with splines please. I've managed to get splines working so far where the 'player' will follow the select route.
However I would like to find out how to make the 'player' switch splines when needed, for example while running they can choose a different path and not to just appear on it- more like a jumping motion.
I've tried following other posts but I simply can't get the hang of it at all. If it makes things easier I'm trying to create a rail shooter/runner.
Any help on the subject is greatly appreciated and thank you in advance.
Post the code that you have and that does not work how you want, and the community can help you.
Basically I'm using an invisible gameObjects to follow me the opposite spline. Hope this gives you guys an idea.
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveTo : $$anonymous$$onoBehaviour {
// private Transform myTransform;
public GameObject ObjectA;
public GameObject ObjectB;
public float smooth = 5.0F;
// Update is called once per frame
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
transform.position = Vector3.Lerp(transform.position, ObjectB, Time.deltaTime * smooth);
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
transform.position = Vector3.Lerp(transform.position, ObjectA, Time.deltaTime * smooth);
}
}
Answer by Graham-Dunnett · Jan 23, 2013 at 08:13 PM
Okay, so you currently use two GOs and can get the player or whatever the script is attached to to move between them. You're using Lerps, which is not quite the same thing as a spline. But no matter. So, instead of having ObjectA and ObjectB, have an array of GameObjects. You can then in the inspector populate this array with all the invisible game objects that define where the player can get to. Then, in your code, use ObjectA and ObjectB. These would be used as the start and end points for the player. Your challenge is then to work out how to set these two values.
To keep things simple, assume that you have only 3 invisible game objects, call them go1, go2 and go3 in the array. To start with, ObjectA = go1, and ObjectB = go2. Your player can move between them. If the player gets to go2, then set ObjectA = go2, and ObjectB = go3. The player then moves between these.
If your player can only move between a linear sequence, say go1->go2->go3->...->goN then these transitions are easy, ObjectA = ObjectB and ObjectB = whatever it was pointing at plus one. If the ordering is more complex, so the player can progress through a graph network then your transitions are harder to work out. The principle is the same, just you'll need to compute the new values for ObjectA and ObjectB. That might depend on ObjectB, and perhaps what button or key the user has pressed. If you look at the diagram at the top of the wikipedia link, assume that the player starts at 6. They move towards 4. When they get there, if they press the space bar, they do a jump, and move towards 5. If they are not pressing space, then they move towards 3. And so on.
I guess, for completeness, I should have added that the game objects in a graph would be called nodes, and the transitions between nodes are called edges. Your problem is really deciding how to represent the edges, and use those to determine how the player transitions from one edge to the next. You can think of each node as storing data about which edges can be taken depending on which key is pressed.
Your answer
