2016-06-06 4 views
0

Je travaille actuellement sur un jeu de plateforme dans Unity3D qui se déroulerait dans un environnement 3D avec des contrôles 2D (gauche, droite, saut).Caractère XY Path Locked Controllable

J'ai fait des recherches sur Internet et essayé quelques méthodes différentes comme iTween, mais je n'en étais pas satisfait. Ce que j'essaye de créer est un chemin/plan incurvé sur lequel les positions X et Y du joueur seraient bloquées au chemin. Le joueur serait capable de marcher sur le chemin, comme n'importe quel autre jeu de plateforme, la spline me permettrait seulement d'utiliser l'environnement 3D mieux et de montrer un monde immersif sous différents angles. L'axe Z, cependant, serait libre, permettant au joueur d'aller aussi haut ou bas qu'ils le voudraient.

Je ne sais pas exactement comment procéder et je me demandais si vous aviez des conseils ou des idées sur la meilleure façon de le faire.

Merci, Septos

+0

S'il vous plaît montrer vos tentatives actuelles à résoudre vous-même – thesecretmaster

+0

Désolé pour la réponse tardive, ici est le code que j'ai trouvé jusqu'à présent: – Septos

Répondre

0

Désolé pour la réponse tardive, voici le code que j'ai jusqu'à présent:

using UnityEngine; 
using System.Collections; 

public class CharacterPathController : MonoBehaviour 
{ 
    enum Direction {Forward, Backward}; 
    Direction charDirection; 

    public PathEditor pathToFollow; 

    private Vector3 lastPos; 
    private Vector3 charRotation; 
    private Vector3 direction; 

    public string pathName; 
    public int currentWayPointID = 0; 
    public float speed; 
    public float rotationSpeed = 7f; 

    private float reachDistance = 1.0f; 
    private float distanceX; 
    private float distanceZ; 

    void Start() 
    { 
     //pathToFollow = GameObject.Find(pathName).GetComponent<PathEditor>(); 
     lastPos = transform.position; 
    } 

    void Update() 
    { 
     Movement(); 
    } 

    void Movement() 
    { 
     if(Input.GetKey(KeyCode.RightArrow)) 
     { 
      charDirection = Direction.Forward; 

      //Calculates distance between target x and z position and object's 
      if(pathToFollow.pathObj[currentWayPointID].position.x > transform.position.x) 
       distanceX = pathToFollow.pathObj[currentWayPointID].position.x - transform.position.x; 
      else 
       distanceX = transform.position.x - pathToFollow.pathObj[currentWayPointID].position.x; 

      if(pathToFollow.pathObj[currentWayPointID].position.z > transform.position.z) 
       distanceZ = pathToFollow.pathObj[currentWayPointID].position.z - transform.position.z; 
      else 
       distanceZ = transform.position.z - pathToFollow.pathObj[currentWayPointID].position.z; 

      //Lerps object's x and z to target's 
      lastPos.x = Mathf.Lerp(transform.position.x, pathToFollow.pathObj[currentWayPointID].position.x, Time.deltaTime * speed); 
      lastPos.z = Mathf.Lerp(transform.position.z, pathToFollow.pathObj[currentWayPointID].position.z, Time.deltaTime * speed); 
      transform.position = new Vector3(lastPos.x, 0, lastPos.z); 

      //Get target's position and rotates on y-axis 
      Vector3 targetPosition = new Vector3(pathToFollow.pathObj[currentWayPointID].position.x, 
               this.transform.position.y, 
               pathToFollow.pathObj[currentWayPointID].position.z); 
      this.transform.LookAt(targetPosition); 

      if(distanceX <= reachDistance && distanceZ <= reachDistance) 
       currentWayPointID++; 
     } 
     else if(Input.GetKey(KeyCode.LeftArrow)) 
     { 
      charDirection = Direction.Backward; 

      //Calculates distance between target x and z position and object's 
      if(pathToFollow.pathObj[currentWayPointID - 1].position.x > transform.position.x) 
       distanceX = pathToFollow.pathObj[currentWayPointID - 1].position.x - transform.position.x; 
      else 
       distanceX = transform.position.x - pathToFollow.pathObj[currentWayPointID - 1].position.x; 

      if(pathToFollow.pathObj[currentWayPointID - 1].position.z > transform.position.z) 
       distanceZ = pathToFollow.pathObj[currentWayPointID - 1].position.z - transform.position.z; 
      else 
       distanceZ = transform.position.z - pathToFollow.pathObj[currentWayPointID - 1].position.z; 

      //Lerps object's x and z to target's 
      lastPos.x = Mathf.Lerp(transform.position.x, pathToFollow.pathObj[currentWayPointID - 1].position.x, Time.deltaTime * speed); 
      lastPos.z = Mathf.Lerp(transform.position.z, pathToFollow.pathObj[currentWayPointID - 1].position.z, Time.deltaTime * speed); 
      transform.position = new Vector3(lastPos.x, 0, lastPos.z); 

      //Get target's position and rotates on y-axis 
      Vector3 targetPosition = new Vector3(pathToFollow.pathObj[currentWayPointID - 1].position.x, 
               this.transform.position.y, 
               pathToFollow.pathObj[currentWayPointID - 1].position.z); 
      this.transform.LookAt(targetPosition); 

      if(distanceX <= reachDistance && distanceZ <= reachDistance) 
       currentWayPointID--; 
     } 

     if(currentWayPointID >= pathToFollow.pathObj.Count) 
     { 
      //Put code to finish the level 
      currentWayPointID = 6; 
     } 
     else if(currentWayPointID < 0) 
      currentWayPointID = 0; 
    } 
}