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 Gibbodoodah · May 21, 2015 at 03:24 PM · gameball

Getting the cursor to follow an object - Unity C#

Hi Guys

I've been having some problems getting the cursor to follow a "Ball" (simple sphere). They Y-axis works ok (with a slight deviation as the cursor goes higher or the screenor lower) but the x-axis is followed by the cursor in the wrong direction i.e. the cursor goes left and the ball goes right.

Any help would be really appreciated!

 private Vector3 Ball;
 private float speed = 15.0f;
 
 void Start()
 {
      Ball = transform.position;
 }
 
 void Update () 
 {
      float zValue = transform.position.z - Camera.main.transform.position.z;
      Ball = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zValue);
      Ball = Camera.main.ScreenToWorldPoint(Ball);
      transform.position = Vector3.Lerp (transform.position, Ball, speed * Time.deltaTime);
 }
 
Comment
Add comment · Show 6
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 Cherno · May 21, 2015 at 04:09 PM 0
Share

Have you tried inverting the x value?

 -Input.mousePosition.x
avatar image Gibbodoodah · May 21, 2015 at 05:03 PM 0
Share

Yes and it weirdly sends the ball to -20 in the x direction, I've tried changing the x value many times and nothing will change it apart from Input.mousePosition.x

avatar image Gibbodoodah · May 24, 2015 at 03:15 PM 0
Share

UPDATE

I have just realised that I have rotated the camera 180 degrees because in my game there are 2 cameras (one for player 1 and one for player 2).

The code works for the camera that is first created but when I add another camera I have to rotate it 180 degrees in the Y-axis to get it to face the opposite way...

Is there any way I can fix my code or the way my camera is set up so that the original error doesn't happen?

Thanks

avatar image TheDarkVoid · May 24, 2015 at 03:27 PM 0
Share

oh, in that case you'll need a direct reference to the camera you want the object to move in relation to. I'm assu$$anonymous$$g that your main camera isn't the one that you are looking through when trying to have the ball follow the cursor. I updated my answer to reflect this.

avatar image Gibbodoodah · May 24, 2015 at 03:55 PM 0
Share

Thanks for your input it really helps and I can genuinely see I'm making progress but now I have an error saying that the variable 'cam' of the script has not been assigned.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDarkVoid · May 21, 2015 at 05:18 PM

Your original code seems to work just fine, apart from some issues with camera rotation which may be the source of your problem. The updated code below should solve it.

 public Camera cam; //Set this to your camera
 private Vector3 Ball;
 private float speed = 15.0f;
 private float zValue;

 
 void Start()
 {
     Ball = transform.position;
     zValue = Mathf.Abs(transform.position.z - cam.transform.position.z);
 }
 
 void Update()
 {
     Ball = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zValue);
     Ball = cam.main.ScreenToWorldPoint(Ball);
     transform.position = Vector3.Lerp(transform.position, Ball, speed * Time.deltaTime);
 }
Comment
Add comment · Show 1 · 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 Gibbodoodah · May 24, 2015 at 02:51 PM 0
Share

Unfortunately this has not solved the problem, the effects are still the same. $$anonymous$$aybe I might have some settings wrong or something? Can't think of anything really.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

3D Water Drop / Elastic Ball 1 Answer

Adding Force to a rolling ball 1 Answer

Flick soccer: Code correct but ball refuses to turn left/right 2 Answers

getting udp package info inside unity (GlovePIE) 0 Answers

Ball Falling Game. 2 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