Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move the hat...

Discussion in 'Scripting' started by kimi81, Jun 10, 2015.

  1. kimi81

    kimi81

    Joined:
    Feb 28, 2015
    Posts:
    6
    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;
    }
    }
     
  2. kimi81

    kimi81

    Joined:
    Feb 28, 2015
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KorpaController : MonoBehaviour {
    5.  
    6.     public Camera cam;
    7.  
    8.     private float maxWidth;
    9.     private bool canControl;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (cam == null) {
    14.             cam = Camera.main;
    15.         }
    16.         canControl = false;
    17.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    18.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    19.         float korpaWidth = renderer.bounds.extents.x;
    20.         maxWidth = targetWidth.x - korpaWidth;
    21.     }
    22.    
    23.  
    24.     void FixedUpdate () {
    25.         if (canControl){
    26.             Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
    27.             Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f);
    28.             float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
    29.             targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
    30.             rigidbody2D.MovePosition (targetPosition);
    31.         }
    32.     }
    33.     public void ToggleControl (bool toggle){
    34.         canControl = toggle;
    35.     }
    36. }
     

    Attached Files: