- Home /
How to move the hat to the edge of the screen?
In this game (like in a catch game tutorial), hat is suppposed to move to the edge of the screen, but not in my case. You can move the hat left and right, but he is not touching the edge of the left side or the right side of the screen. Can someone please tell me what to change in the script in the way that hat can catch all the items that are falling from the sky. Thanks in advance....
PS (korpa stands for a hat)
This is my controller script: using UnityEngine; using System.Collections;
public class KorpaController : MonoBehaviour {
public Camera cam;
private float maxWidth;
private bool canControl;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
canControl = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
float korpaWidth = renderer.bounds.extents.x;
maxWidth = targetWidth.x - korpaWidth;
}
void FixedUpdate () {
if (canControl){
Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f);
float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
rigidbody2D.MovePosition (targetPosition);
}
}
public void ToggleControl (bool toggle){
canControl = toggle;
}
}
Answer by jmjohnson1 · Apr 11, 2017 at 05:41 PM
This happened to me as well. The hat in the updated 2D pack has extra space surrounding the hat. To fix this, go to HatSprite in the Project panel. On the Inspector panel, change Pixels Per Unit to 200 and make sure the Mesh Type is set to "Tight." Look a little further down and open the Sprite Editor. Put 128 in each of the border fields to trim it down and close it. Head over to the HatSprite in the Hierachy panel. Make sure the Draw Mode in the Inspector panel is set to Sliced. That fixed it for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200937im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Funky Glowing Things 1 Answer
Any good iOS tutorials out there?? 1 Answer
Unity FPS Tutorial 0 Answers
3d Platformer Tutorial is not a Unity project folder. 1 Answer