Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zeman97 · Dec 26, 2014 at 12:53 AM · uiguipositionbug4.6

v4.6 UI Element's Positioning Incorrect

Hello, I am writing a script that allows a cursor UI Element to follow an ingame object. The purpose of this is to allow me to control the cursor with both a gamepad and a mouse. Here is the code (Attaches to the UI Element):

 using UnityEngine;
 using System.Collections;
 
 public class CursorController : MonoBehaviour
 {
     public Transform lookAtPoint;
     public Camera playerCamera;
 
     void Update()
     {
         //This value is correct.
         Vector3 position = Camera.main.WorldToScreenPoint(lookAtPoint.position);
         Debug.Log(position);
 
         //This sets the UI object to a very wrong position.
         GetComponent<RectTransform>().position = position;
         Debug.Log(GetComponent<RectTransform>().position);
 
         //This is more accurate but still off.
         GetComponent<RectTransform>().localPosition = position;
         Debug.Log(GetComponent<RectTransform>().localPosition);
 
         //If you run the code, you will likely notice that all of the Debug Logs write the same values, despite the values not being the same in practice.
     }
 }

The lookAtPoint GameObject is working perfectly, according to the Debug Console, so should the cursor. The cursor used to work and then today I opened Unity and is has stopped working. If anyone can help it will be greatly appreciated. Thanks!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wowipop · Dec 26, 2014 at 04:35 AM 0
Share

,Is your canvas render mode set to Overlay, world space or camera?

avatar image zeman97 · Dec 26, 2014 at 05:02 AM 0
Share

Canvas is set to screen space - camera, I cant use overlay because I am setting up slit screen, however tested with overlay, still doesn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by wowipop · Dec 26, 2014 at 04:35 AM

Easiest way is to set your canvas to Overlay, then set the anchor of the recttransform to lower left and

      Vector3 position = Camera.main.WorldToScreenPoint(lookAtPoint.position);
      GetComponent<RectTransform>().position = new Vector2(position.x,position.y);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zeman97 · Dec 26, 2014 at 05:16 AM 0
Share

Your code and suggestions work for the cursor, but not the rest of the UI, to fix this I moved the cursor to a separate canvas that uses screen-overlay and the rest of the UI sits in a screen-camera, the cursor stays where it needs to and the UI moves with the camera to allow for splitscreen, thanks!

avatar image wowipop · Dec 26, 2014 at 06:06 AM 0
Share

Because this script only move the RectTransform that is attached to. and the CursorController is attached to the cursor.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

(4.6 UI) How to set up a window with x buttons, with a scroll bar? 1 Answer

uGUI Image component not auto-generating mesh around sprite? 0 Answers

New 4.6 UI question: How do you resize a panel relative to the screen but remain height/width constrained? 5 Answers

Unity 4.6B UI Scrollbar Usage 1 Answer

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges