Like… you’re tweening a GameObject (but not) to call a onupdate event to update a variable that is during OnGUI (why OnGUI I don’t know…) moving a gameObject along a path defined by an array of transforms…
Why?
Like, sure, it gets the job done. But it’s like if you were trying to mow your lawn, so you call your neighbour to cut down his tree, which falls in your yard, so that the city then shows up to clean up the mess, and in the process mows your yard.
Talk about the long way around.
So yeah, I’m not surprised you’re having a hard time figuring out where to stick code to change the speed between nodes. You’re not animating from node to node… you’re animating a percentage, and then resolving the position from that.
I legitimately do not know how to convey to you how to do this without just ripping your code out and replacing it with more manageable code… and why fore should I rewrite your software for you?
…
I agree with Ryiah:
note the keyword… ‘multiple’ paths.
Not singular like you reply:
Multiple paths.
Then each one has its own time value which effects the speed for each individual path. Which you then play in sequence.
…
But then again, this path appears to be a spline? Like a catmull-rom or something? Which means you need each node…
ugh.
How do I explain this then… personally I don’t use iTween, I use my own tween engine, and I have an easy way to animate over splines that also give you the current part of the path you’re on so you can do this sort of thing.
iTween, might have that? I don’t know. And I don’t even know where to begin to explain it.
Thanks a lot for the reply. The reason why the code is a bit weird is because it’s from the examples and I changed it a little to my desire. I will try to make my own code and use multiple paths.
I think the main problem for me is that I attatch the code to the camera itself, so I can’t make multiple paths with that.
I tried to control the camera from another gameobject but that didn’t work, I don’t know why:
Changed the ‘‘gameObject’’ to MainCamera to controll it from another gameobject.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FlythroughCameraController : MonoBehaviour {
public Transform[] movePath;
public Transform[] lookPath;
public Transform lookTarget;
public float percentage;
public GameObject percentageTextObj;
//private float redPosition = .16f;
//private float bluePosition = .53f;
//private float greenPosition = 1;
private float end = 1;
public float time;
//gui styling
public Font font;
private GUIStyle style = new GUIStyle();
public GameObject player;
public GameObject MainCamera;
Text percentageText;
void Start(){
style.font=font;
}
void Update()
{
percentageText = percentageTextObj.GetComponent<Text> ();
Player playerScript = player.GetComponent<Player> ();
percentageText.text = Mathf.Round(percentage * 100f) + "%";
if (playerScript.starting == 1)
{
SlideTo (end);
}
}
void OnGUI(){
//percentage=GUI.VerticalSlider(new Rect(Screen.width-20,20,15,Screen.height-40),percentage,1,0);
iTween.PutOnPath(MainCamera,movePath,percentage);
iTween.PutOnPath(lookTarget,lookPath,percentage);
transform.LookAt(iTween.PointOnPath(lookPath,percentage));
//if(GUI.Button(new Rect(5,Screen.height-25,50,20),"Red")){
// SlideTo(redPosition);
//}
//if(GUI.Button(new Rect(60,Screen.height-25,50,20),"Blue")){
// SlideTo(bluePosition);
//}
//if(GUI.Button(new Rect(115,Screen.height-25,50,20),"Green")){
// SlideTo(greenPosition);
//}
}
void OnDrawGizmos(){
iTween.DrawPath(movePath,Color.magenta);
iTween.DrawPath(lookPath,Color.cyan);
Gizmos.color=Color.black;
Gizmos.DrawLine(MainCamera.transform.position,lookTarget.position);
}
void SlideTo(float position){
iTween.Stop(MainCamera);
//"easetype",iTween.EaseType.easeInOutCubic
iTween.ValueTo(MainCamera,iTween.Hash("from",percentage,"to",position,"time",time,"easetype","linear","onupdate","SlidePercentage"));
Player playerScript = player.GetComponent<Player> ();
playerScript.starting += 1;
}
void SlidePercentage(float p){
percentage=p;
}
}
Actually I think that after I solve this problem I say to myself: Why did I do such stupid things…
public GameObject lookTarget;
public GameObject MainCamera;
[System.Serializable]
public class Paths
{
public float time;
public Transform[] movePoints;
}
public Paths[] Path;
With this I can make multiple paths and adjust the time of each path right?
public GameObject lookTarget;
public GameObject MainCamera;
public GameObject nextMovePoint;
private int currentPoint = 0;
[System.Serializable]
public class Paths
{
public float time;
public Transform[] movePoints;
}
public Paths[] Path;
void Update()
{
Paths paths = gameObject.GetComponent<Paths>();
Debug.Log (paths.movePoints [currentPoint]);
if (nextMovePoint.transform.position != paths.movePoints [currentPoint].position)
{
currentPoint += 1;
nextMovePoint.transform.position = paths.movePoints [currentPoint].position;
}
iTween.MoveTo(nextMovePoint,iTween.Hash("time",paths.time));
}
I don’t know if this will work but I think that it will go from one point to another???
Also I don’t know if it switches between elements in the array.
But I do get this error:
ArgumentException: GetComponent requires that the requested component ‘Paths’ derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponent[Paths] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:35)
CameraMove.Update () (at Assets/Scripts/CameraMove.cs:29)
Paths isn’t a component, it’s just a class. You understand that a component is something you add to a gameobject? Such as a collier or audio source. Scripts are also a component but they need to inherit from monobehaviour at some point to use that command.
Thanks, I understand what you mean, but I don’t understand how I could use paths.movePoints, without ''GetComponent". I found this answer from another thread:
Unity uses a component model. Your class exists as a component of whatever GameObject you put it on. All components extend Monobehaviour which provides a reference to the GameObject and many other common components. If you want to access another class, you have to find a GameObject on which it’s resides, and then retrieve it from that GameObject via some manner of GetComponent. And that’s only if you have to do it via scripting. It’s far easier to make public references. In your GroundCheck class you would have a public PlayerMovement object. Then in the editor you can drag any object that has a PlayerMovement script into that property on GroundCheck component. Now your groundcheck can just access that. Unity’s really fun that way because your code can be treated like any other asset.
But I still don’t understand what to do with my code.