Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Aug 08, 2021 at 05:33 PM by ebraama06 for the following reason:

The code is working. Read my reply to the first comment to know why.

avatar image
1
Question by ebraama06 · Aug 07, 2021 at 03:59 PM · cameramousepositionlearninglearn

move camera relative to mouse position.

I want to make the camera stake to the player so i do this:

 transform.position = player.transform.position + offset;

making offset to give some space between the player and the camera.

Now I want to the camera move slightly to the position of the mouse to give more vision to the player so I added this:

         Vector3 mousePos = Input.mousePosition;
         mousePos.z = Camera.main.nearClipPlane;
         Vector3 worldPosition = Camera.main.ScreenToViewportPoint(mousePos);
         worldPosition = new Vector3(worldPosition.x - 0.5f , worldPosition.y - 0.5f ,0);
         worldPosition *= 2;

making the z position 0 so the player character to look at the curser in the future.

deduction 0.5 from X and Y to make the worldPosition in the centerof the screen.

but i can't add worldPosition to player.transform.position and offset becouse it will constantly add the worldPosition making the camera fly off the scene where it should be.

how do i add these variables to gather.

here is the full script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraMovement : MonoBehaviour
 {
     [SerializeField] private GameObject player;
     [SerializeField] private Vector3 offset;
 
     private void Update()
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = Camera.main.nearClipPlane;
         Vector3 worldPosition = Camera.main.ScreenToViewportPoint(mousePos);
         worldPosition = new Vector3(worldPosition.x - 0.5f , worldPosition.y - 0.5f ,0);
         worldPosition *= 2;
         Debug.Log(worldPosition);
         
         
         transform.position = player.transform.position + offset + worldPosition;
     }
 
 }

and thank you for reading

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 CodesCove · Aug 08, 2021 at 12:11 AM 0
Share

"but i can't add worldPosition to player.transform.position and offset" Why would you want to add anything to the player.transform.position? I maybe missing your point but can you clarify this.

Anyway the code you posted should work just fine and nothing should "fly off"

Just make sure this script is attached to the Game Object where the Camera component is and not to the Player. If so then things certainly will "fly off"

avatar image ebraama06 CodesCove · Aug 08, 2021 at 05:29 PM 0
Share

lol! I forgot to test my code yesterday so here is what happened: yesterday I spent all of time playing and practicing unity and translating some stuff without taking any breaks form the screen or sitting so i wanted and end this script and forgot to test it after fixing some syntax problem. for wasting your time. and i am going to this post for anyone who want it because i spent all of my time researching for it. Thank you

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

192 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Raycast object flies towards mouse on screen, i need it on floor 1 Answer

Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers

Make object follow mouse, while actually moving camera, instead of object 2 Answers

MousePosition input problem after rotating object. 0 Answers

ScreenToWorldPoint exclude objects or layers? 1 Answer


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