Space between sprites
So I made a simple 2D setting where you can move a block. But I can not completely snap to another sprite (wall).
Screenshot:
Inspector settings wall/player:
PlayerMovement Script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public KeyCode moveUp;
public KeyCode moveDown;
public KeyCode moveLeft;
public KeyCode moveRight;
public float speed = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
Vector2 v = rigidbody2D.velocity;
if (Input.GetKey (moveUp)) {
v.y = speed;
v.x = 0;
rigidbody2D.velocity = v;
} else if (Input.GetKey (moveDown)) {
v.y = speed * -1;
v.x = 0;
rigidbody2D.velocity = v;
} else if (Input.GetKey (moveRight)) {
v.x = speed;
v.y = 0;
rigidbody2D.velocity = v;
} else if (Input.GetKey (moveLeft)) {
v.x = speed * -1;
v.y = 0;
rigidbody2D.velocity = v;
}
else
{
v.x = 0;
v.y = 0;
rigidbody2D.velocity = v;
}
}
}
These are the files I'm using: Player: https://onedrive.live.com/redir?resid=451ADC2A2ED2D0E7!108&authkey=!AAimm1ss1vE_ZfA&v=3hint=photo%2cpng Wall: https://onedrive.live.com/redir?resid=451ADC2A2ED2D0E7!109&authkey=!AAxqJwKjBiS2n8A&v=3hint=photo%2cpng
I have no idea what can create this space because the this is my first unity game. Thanks in advance!!
Answer by Abhiroop-Tandon · Mar 30, 2016 at 11:12 AM
Make sure there is no extra space in the sprite that is causing this issue to be sure. If there is no space then what you can do is press V when you are in the 'moving the object ' mode and hover at one corner of your object. You will see some kind of a pointer over there. If you drag from that pointer you can snap it at any edge of the other object with utmost precision. i hope it makes sense. Just try it and you will understand what i am trying to say. Also make sure the collider is not bigger than your sprite.
PS- You need a mouse for this, i dont know why it doesnt works on the touchpad of the laptop.
Hi, I can snap the player to the wall when I am in the scene mode, but the problem occurs when I play the game and try to move the square to a wall, I added the sprites I used in my post and there is no space left.
When I play the game and then try to touch the wall I can see the colliders are not touching each other in the scene view as you can see here: http://prntscr.com/alyntu
i hope you can help me as I am very frustrated!
Did you double check the colliders on both the player and the wall ?? Try reducing their size and see what happens. Dont worry making a game is a long and intense process. Just hang in there and you will do it.
Reducing the size does not help. I checked the colliders and they are at the right point. But I they can;t touch each other/ As you can see in my screen shot (The green lines are the colliders). Thanks for your help I highly appreciate it!
Just one quick thing, is your player following physics ?? If its not then why do you have a rigidBody on it?
Does your player needs to be affected by gravity? You have no parameters set for that under the rigidbody component. Try adding mass and gravity to get it down on the ground.
If your player does not needs to be affected by gravity then it should be kinematic. You should check that 'is $$anonymous$$inematic' box under the rigidbody component.
Try out these things and let me know.
Hi, I need the rigid body to detect Collision. But when I have set the rigidbody to use gravity it also shows this space. Also with other colliders like Circle colliders and stuff. But when I tick the kinematic option it also shows me the space.