1
votes

i need to create a skid or slips effect. I want create the skid or slips effect as in sunset overdrive :

enter image description herehttp://i.stack.imgur.com/vmx8f.jpg

when the main character slips on the electrical cables and on the rails. how can I create the same result? in C#. See this video: https://www.youtube.com/watch?v=zXjNkoQQPls (from 1.20 min to 1.25) . I have create this code: -OnTrigger- (apply to electric cables)

using UnityEngine; using System.Collections; public class OnTriggerEnterScroll : MonoBehaviour {

/*ISTRUZIONI : Inserire questo Script all'interno dell'Elemento che entra in contatto con il Trigger */

public Transform target; // Player
AutoMove AutoMoveScript; // richiamo variabili Script AutoMove

// Use this for initialization
void Start () {
    AutoMoveScript =  target.GetComponent<AutoMove>(); // Richiano componente AutoMove
}

// Update is called once per frame
void Update () {
    Vector3 targetDir = target.position - transform.position;
    Debug.Log(targetDir);
}

void OnTriggerEnter(Collider other) { // Quando il player entra nel Trigger
    if(other.collider.name == "MainCharacter"){

            AutoMoveScript.MoveSpeed = 8;  // Cambio variabile di Velocita'
            AutoMoveScript.activeAutoMoveW = true; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveS = false; // Cambio variabile di scorrimento


    }

}

void OnTriggerStay(Collider other) { // Quando il player e' nel Trigger
    if(other.collider.name == "MainCharacter"){
        if(Input.GetKey(KeyCode.W)){

            AutoMoveScript.activeAutoMoveW = true; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveS = false; // Cambio variabile di scorrimento

        }

        if(Input.GetKey(KeyCode.S)){

            AutoMoveScript.activeAutoMoveS = true; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveW = false; // Cambio variabile di scorrimento

        }

    }

}

void OnTriggerExit(Collider other) { // Quando il player esce nel Trigger

    if(other.collider.name == "MainCharacter"){

            AutoMoveScript.MoveSpeed = 8; // Cambio variabile di Velocita'
            AutoMoveScript.activeAutoMoveS = false; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveW = false; // Cambio variabile di scorrimento

    }

}
}

-AutoMove- (apply on main Character)

using UnityEngine; using System.Collections; public class AutoMove : MonoBehaviour {

/*ISTRUZIONI : Inserire questo Script all'interno del Player */

public bool activeAutoMoveW = false; //variabile controllo Forward
public bool activeAutoMoveS = false; //variabile controllo Back

public int MoveSpeed = 8;   //variabile velocità di scorrimento
public bool activeSelect = false; //variabile controllo Select


// Use this for initialization
void Start () {


}

// Update is called once per frame
void Update () {

    if(activeAutoMoveW == true)
    {
        transform.Translate(Vector3.forward * MoveSpeed * Time.fixedDeltaTime, Space.World); // Scorrimento in avari
    }

    if(activeAutoMoveS == true)
    {
        transform.Translate(Vector3.back * MoveSpeed * Time.fixedDeltaTime, Space.World); // Scorrimento in  dietro
    }

    if(Input.GetKeyDown(KeyCode.Q) && activeSelect == false)
    {
        MoveSpeed = 1; // Cambio variabile di scorrimento
        activeSelect = true;  // Cambio variabile Slect

    }else if(Input.GetKeyDown(KeyCode.Q) && activeSelect == true)
    {
        MoveSpeed = 8; // Cambio variabile di scorrimento
        activeSelect = false; // Cambio variabile Slect

    }

}   }

The system create the slips effect but don't understand what is the direction of the character.

1

1 Answers

0
votes

If i understand correctly the problem is that you dont know what forward is when you want to slip. The problem with use Vector3.forward in world space like you do in your example is that it will always be the same direction no matter how the object is oriented. The problem with using Vector3.forward in object space is that its the same for the whole object so if you have a curved object like you do in the picture it wont work either.

You need some way to know how the object is curved, one simple example of this is to have a vector3 array of points placed manually along side the rail and then you can calculate the direction by points[i+1]-points[i] with the two points you are between. Assuming you dont always start for the beginning of the rail, you also need some way to find which points you are in between which may not be easy.

So something like this

public class AutoMove : MonoBehaviour 
{
  public Vector3[] points;
  private int currentIndex = -1;
  ...

  void Update()
  {
    if(activeAutoMoveW == true)
    {
        Vector3 direction = points[i+1]-points[i];
        transform.position += direction * MoveSpeed * Time.fixedDeltaTime;

        if((transform.position - points[i+1]).sqrMagnitude < 0.1f) //check if we are at the next point
            currentIndex++;
    }
    if(activeAutoMoveS == true)
    {
        Vector3 direction = points[i+1]-points[i];
        transform.position -= direction * MoveSpeed * Time.fixedDeltaTime;

        if((transform.position - points[i]).sqrMagnitude < 0.1f) //check if we are at the next point
            currentIndex--;
    }

    if(currentIndex < 0 || currentIndex > points.Length) //check if we are at either edge of the line and then end the slip
    {
        activeAutoMoveW = false;
        activeAutoMoveS = false;
    {

    ...
  }

  public void BeginSlip(Vector3[] points) //called from the object when you want to begin the slip
  {
    this.points = points; //get the slip points from the object you slip on

    //find the closest point
    float minDist = float.MaxValue;
    for(int i=0; i<points.Length; i++)
    {
      float currentDist = (transform.position - points[i]).sqrMagnitude;
      if(currentDist  < minDist)
      {
        minDist = currentDist;
        currentIndex = i;
      }
    }
  }
}