Hi there.
I am using the supersplines asset store package to animate an object along a spline. Have contacted them as well but I’m wondering if this is a scripting question anyway.
Currently, an object like a capsule will animate along its short axis along a spline. I can’t change this using the GUI settings on the spline package atm.
I’d like to be able to animate it along its long axis.
I’d like to be able to animate different objects along the spline and set their rotations to different values. Currently I’m using this code to start the object animating (it gets hit by another object).
using UnityEngine;
using System.Collections;
public class Colliding : MonoBehaviour {
private SplineAnimator cubeL5;
private SplineAnimator capsuleL4;
// Use this for initialization
void Start () {
cubeL5 = GameObject.Find ("CubeL5").GetComponent<SplineAnimator> ();
capsuleL4 = GameObject.Find ("Capsule").GetComponent<SplineAnimator> ();
}
// Update is called once per frame
void OnTriggerEnter (Collider col)
{
if (col.collider.tag == "Player")
{
col.GetComponent<MeshRenderer>().renderer.enabled = false;
cubeL5.transform.renderer.enabled = true;
cubeL5.speed = 0.4f;
cubeL5.tag = "Player";
capsuleL4.transform.renderer.enabled = true;
capsuleL4.transform.eulerAngles = new Vector3(0, 0, 90);
capsuleL4.speed = 0.4f;
capsuleL4.tag = "Player";
}
}
}
I’ve tried adding in
capsuleL4.transform.eulerAngles = new Vector3(0, 0, 90);
to try and set the objects rotation just before it starts to animate but it seems to get overridden by the splineanimator script.
Is there a way to rotate an object in space within Unity and then recalculate its rotation so it reads 0, 0, 0 like you can do in modelling software? That way I can use the GUI to set different rotations on the objects and the Splineanimator script won’t know any different?
Any help appreciated,
A
Definitely isn’t the best way but you could create a empty Gameobject that is animated on your spline. And make your capsule child of the empty game object; and set it’s rotation axis to what you want
Wow well that seems to work well actually, thank you! Are you able to offer tips on to how to modify my code to get a handle onto the child GameObjects so that I can switch on their mesh renderers when needed, like previously?
I’m using an empty with the splineanimator script attached, - ‘TestEmpty’ in the hierarchy
and two planes, - ‘Plane’ and ‘Plane2’ as children.
I’ve tried this but am getting errors.
using UnityEngine;
using System.Collections;
public class Colliding : MonoBehaviour {
private SplineAnimator cubeL5;
private SplineAnimator testEmpty;
private bool plane;
// Use this for initialization
void Start () {
cubeL5 = GameObject.Find ("CubeL5").GetComponent<SplineAnimator> ();
testEmpty = GameObject.Find ("TestEmpty").GetComponent<SplineAnimator> ();
plane = GameObject.Find ("Plane").GetComponent<MeshRenderer> ();
}
// Update is called once per frame
void OnTriggerEnter (Collider col)
{
if (col.collider.tag == "Player")
{
col.GetComponent<MeshRenderer>().renderer.enabled = false;
cubeL5.transform.renderer.enabled = true;
cubeL5.speed = 0.4f;
cubeL5.tag = "Player";
testEmpty.speed = 0.4f;
testEmpty.tag = "Player";
plane.renderer.enabled = true;
}
}
}
What errors are you getting?
Assets/FEMORAL/Colliding.cs(29,31): error CS1061: Type bool' does not contain a definition for
renderer’ and no extension method renderer' of type
bool’ could be found (are you missing a using directive or an assembly reference?)
I think I’m setting up my variable wrong with a boolean?
privatebool plane;
plane.renderer.enabled=true;
A bool does not have a rendering component…
yup,
you’ve said plane is a bool: private bool plane;
a bool can only be true or false. It does not have a renderer
I’m using code from previously that was GUIText and it worked, very inexperienced with this. So can you help me set up the plane to get a handle on its renderer component? Can I just use the meshrenderer class?
private MeshRenderer plane;
Yes you can. Use meshrenderer, or the gameobject.
private MeshRenderer planeRenderer;
planeRenderer =GameObject.Find("Plane").GetComponent<MeshRenderer>();
planeRenderer.enabled=true;
private GameObject planeGameObject;
planeGameObject=GameObject.Find("Plane");
planeGameObject.renderer.enabled=true;
Either one of those should work.
Magic, thank you very very much both
1 Like