- Home /
Collision Not Responding
Hello i am making a C Road Puzzle Game and i have Player Movement Code[C#] here is my code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bounce : MonoBehaviour {
public Swipe swipeControls;
public GameObject key;
float lerpTime ;
float currentLerpTime;
float perc = 1;
Vector3 startPos;
Vector3 endPos;
bool FirstInput;
public bool justJump;
void Update ()
{
if(swipeControls.SwipeLeft || swipeControls.SwipeRight || swipeControls.SwipeUp || swipeControls.SwipeDown)
{
if(perc == 1)
{
lerpTime = 1;
currentLerpTime = 0;
FirstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
if (swipeControls.SwipeRight && gameObject.transform.position == endPos)
{
endPos = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
}
if (swipeControls.SwipeLeft && gameObject.transform.position == endPos)
{
endPos = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
}
if (swipeControls.SwipeUp && gameObject.transform.position == endPos)
{
endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
}
if (swipeControls.SwipeDown && gameObject.transform.position == endPos)
{
endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
}
if (FirstInput == true)
{
currentLerpTime += Time.deltaTime * 5;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
}
if(perc > 0.8)
{
perc = 1;
}
if (Mathf.Round(perc) == 1)
{
justJump = false;
}
}
void OnTriggerEnter(Collider myTrigger)
{
if(myTrigger.gameObject.name == "Enemy")
{
key.SetActive(false);
}
}
}
The Problem is that my Player cant Collide with other objects it can only collide using Rigid Body which i do not want in my game,Can someone EDIT my code so Character Controller will be needed so it can collide.Please i am no expert by the way this is my 2nd game.
You are moving the Object with transform, meaning you kinda teleporting it to new position. That way the physics system does not work thus no collisions happen, think it like you move an object on Editor , you may place it wherever you want regardless colliders, this is pretty much the same.
To solve it either use Physics (Rigidbody) and assosiated functions to move the obj or in the current code apply a check to see if new location is occupied before moving, but still there will be no actual collisions happen that way. Cheers.
Answer by hannounq · Sep 16, 2017 at 09:21 AM
did you add a 2D collider in the unity editor? Thats where i usually add my colliders.