- Home /
Non - Physics collision System
OK, so I have a cube using the new physics 2D toolset in Unity 4.3 and I control it using vector2 and transform and I have it line cast to detect if their is an object to the sides, front, and back. That works but when it detects an object on the solid layer I can't figure out how to make the player stop moving. Here is my script. #pragma strict
var speed : float; //speed of the player
var PosH : float; //value of change in the Horizontal axis of the controller
var PosV : float; //value of change in the Vertical axis of the controller
var bullet : Rigidbody2D; //Projectile being shot has a Rigidbody2D component
var GunPoint : Transform; //Point at the tip of the gun where the bullet is instantiated
var bulletspeed : float; //Constant speed the bullet goes
var facing: Vector3 = new Vector3(0.0f, 1.0f, 0.0f); //default to facing "up"
var deadzone: float = 0.1;
//
var Right : boolean = false;
var Left : boolean = false;
var Up : boolean = false;
var Down : boolean = false;
//
var RightT : Transform;
var LeftT : Transform;
var UpT : Transform;
var DownT : Transform;
//
function FixedUpdate(){
if ( Mathf.Abs(PosH) > deadzone || Mathf.Abs(PosV) > deadzone) {
facing = new Vector3(PosH, PosV, 0.0f);
facing.Normalize();
}
//
Right = Physics2D.Linecast(transform.position, RightT.position, 1 << LayerMask.NameToLayer("Solid"));
Left = Physics2D.Linecast(transform.position, LeftT.position, 1 << LayerMask.NameToLayer("Solid"));
Up = Physics2D.Linecast(transform.position, UpT.position, 1 << LayerMask.NameToLayer("Solid"));
Down = Physics2D.Linecast(transform.position, DownT.position, 1 << LayerMask.NameToLayer("Solid"));
//
PosH = (Input.GetAxis("Horizontal"));
PosV = (Input.GetAxis("Vertical"));
gameObject.transform.position.x = gameObject.transform.position.x + speed * PosH;
gameObject.transform.position.y = gameObject.transform.position.y + speed * PosV;
gameObject.transform.LookAt(transform.position + Vector3(PosH, PosV, 0.0), Vector3.forward);
gameObject.transform.rotation.y = 0;
gameObject.transform.rotation.x = 0;
if(Input.GetAxis("TriggerRight") > 0){
Shoot();
}
}
function Shoot(){
var bulletInstance = Instantiate(bullet, GunPoint.position, Quaternion.Euler(facing));
bulletInstance.velocity = facing * bulletspeed;
Destroy (bulletInstance.gameObject, 1);
}
Can someone please help me out on this I really need to get this done Im on a time limit in the mean time I'm still trying everything to get this to work right.
If anyone has a way to get 2 2DBoxColliders NOT 2DRIGIDBODIES to collide with each other and not intersect that would also be great and would solve my problem.
Please do not bump questions that are at least a month or so old. There are questions from 2009 that still haven't been answered, so get in line and please be patient :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to make an arrow to become stuck? 1 Answer
Cover mechanic - Restrict movement to box 1 Answer
Keep particles to defined area, destroy on collision 0 Answers
How to check if an object has collided with a specified object from a script 1 Answer