Search Unity

Object switch between 3 paths in 3d space while moving at a constant speed.

Discussion in 'Scripting' started by CreativeUserName, Apr 5, 2014.

  1. CreativeUserName

    CreativeUserName

    Joined:
    Apr 5, 2014
    Posts:
    2
    Hello. I'm really hoping I can find some help with this, despite me only being able to provide the theory of what I'm trying to do.

    So I have this object that I want to follow one of 3 paths at a set constant speed. Each path is individual, and the object should be able to switch between them by left or right key presses on the keyboard. The paths are in 3d. So one might go up while another goes down and a third has no change. When there are these sort of differences, I would turn the ability to switch lanes off. The paths should also be able to rotate and have the object rotate along with them. I also intend to have power-ups that speed you up or slow you down, otherwise I could create it as an animation in a 3d program.


    So. I've looked into Herman spline controller, which does some of what I want. It allows me to atleast move an object and rotate the object, but it's set to move not by speed but by distance (so speed is determained by distance). It also has the problem that I wouldn't know how to switch path. (all of the spline solutions I've looked at has this issue though). I know a bit of unityscript, unfortunately the unityscript version of Hermans spline controller is broken. Then theres the spline controller simply called spline controller. It doesn't seem to work properly, as some of it is obsolete. It does function partially, but I'm having problems getting the object to actually go towards a path and not just rotate. I also looked slightly at itween, but this is something I'm hoping to put on mobile devices and itween is supposedly pretty slow for that. I've also looked a bit at hotween but it doesn't seem to be straight forward in relation to what I'm trying to do.

    I've considered a lot of options and tried a lot of options over the past few weeks which has finally driven me here, to the unity forums, in the hope that someone will try to give me a hand in figuring out how to make this possible..

    Thanks a lot for the time.
    Regards.
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    you could try cubic splines with handles set to 1/3 length between points. That atleast is what gives constant speed for my game.
    my idea would be to let 3 tokens run each on a lane (3 ghost entities) and linear interpolate between them for the real position.
     
  3. CreativeUserName

    CreativeUserName

    Joined:
    Apr 5, 2014
    Posts:
    2

    I did consider something along the lines of creating 3 objects. 1 for each path, but so far I kept coming back to the fact that it would be an issue to have them follow the path and at the same time mantain th exact same position along the path as the two other objects. So what I've done now (so far) is a bit different than the spline solution. I've created an object (going to be two more) that raycasts downwards and repositions itself on the Y axis according to the collider it hits. At the same time, it's also told to follow another object on the z axis that moves at a constant speed It does still leave me with the problem of rotation though. I'm not sure how i can have he raycast shoot back from the collision and then grab the direction of it.

    This is the script so far (for the follower objects):
    Code (csharp):
    1. #pragma strict
    2. var player : GameObject;
    3.  
    4. function Start () {
    5. var depthOffset = -10;
    6. var verticalOffset = 0;
    7.  
    8. }
    9.  
    10. function Update () {
    11.     transform.position.z = player.transform.position.z;
    12.     transform.rotation.y = player.transform.rotation.y;
    13.    
    14.     var hit : RaycastHit;
    15.     if (Physics.Raycast(transform.position, Vector3.down, hit, 100)){
    16.             if (hit.collider.tag == "ground"){
    17.             transform.position.y = hit.point.y +2;
    18.         }
    19.     }
    20. }
    after transform position, i've tried a transform.rotation but without luck. Ofcourse, I assume this is because the original ray cast does not have any rotation, and therefore the ray impact point should shoot a new ray back, this time with it's own rotation as origin to then be grabbed and added to the rotation of this object. Any ideas ?
     
    Last edited: Apr 5, 2014
  4. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    you can use the hit.normal fromt RaycastHit and for example transform.lookAt the Vector3.cross with transform.left (not sure).