Tout d'abord, merci de votre indulgence face à mon ignorance !!!
Je veux qu'un objet ( un cube) se déplace sur l'axe x par exemple quand mon personnage "le pousse" pendant plus d'une seconde et qu'il n'est pas en collision avec un autre cube ou mur.
Je pensais y être arrivé mais régulièrement et sans comprendre pourquoi, le cube se déplace de 2 fois la valeur voulue...
Voici le script du cube :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxScript : MonoBehaviour
{
private Vector3 startPos;
private Vector3 endPosG;
private Vector3 endPosD;
private float distance = 2f;
private float t = 1f;
BoxDroite boxdroite;
BoxGauche boxgauche;
// Start is called before the first frame update
void Start()
{
boxdroite = gameObject.GetComponentInChildren<BoxDroite> ();
boxgauche = gameObject.GetComponentInChildren<BoxGauche> ();
}
void Awake ()
{
}
// Update is called once per frame
void Update()
{
startPos = transform.position;
endPosG = transform.position + Vector3.left * distance;
endPosD = transform.position + Vector3.right * distance;
if(boxdroite.playerPushLeft && !boxgauche.isBlockedByBox && !boxgauche.isBlockedByWall)
{
transform.position = Vector3.Lerp(startPos, endPosG, t );
Debug.Log("move to left");
}
if(boxgauche.playerPushRight && !boxdroite.isBlockedByBox && !boxdroite.isBlockedByWall)
{
transform.position = Vector3.Lerp(startPos, endPosD, t );
Debug.Log("move to right");
}
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxGauche : MonoBehaviour
{
public bool isBlockedByWall;
public bool isBlockedByBox;
public bool playerPushRight;
float t = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider col)
{
if (col.GetComponent<Collider>().tag == "ColliderPlayer")
{
t = 0;
playerPushRight = false;
}
if (col.GetComponent<Collider>().tag == "PushableD")
{
isBlockedByBox = true;
}
if (col.GetComponent<Collider>().tag == "Wall")
{
isBlockedByWall = true;
}
}
void OnTriggerStay(Collider col)
{
t += Time.deltaTime;
if (col.GetComponent<Collider>().tag == "ColliderPlayer" && Input.GetAxisRaw ("Horizontal") > 0 && t > 1f)
{
playerPushRight = true;
}
}
void OnTriggerExit(Collider col)
{
if (col.GetComponent<Collider>().tag == "ColliderPlayer"&& Input.GetAxisRaw ("Horizontal") > 0 )
{
playerPushRight = false;
}
if (col.GetComponent<Collider>().tag == "PushableD")
{
isBlockedByBox = false;
}
if (col.GetComponent<Collider>().tag == "Wall")
{
isBlockedByWall = false;
}
}
}
Merci par avance!!!