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 ARefrigerator · Jul 22, 2014 at 09:11 PM · cameramousemouse movement

Mouse movement relative to screen space

I currently have code wherein I am able to move the camera based on the act of clicking down the left muse button and then dragging the camera. The problem lies in when the pointer is far away or close to the screen the movement is distorted.

Namely I want the mouse to function in a way so that when it is further away from the camera the momentum of moving the camera towards that point is stronger. Likewise pushing away from a close area would push the camera back stronger than if you were to do so from a further point.

I have a raycast for the sake of placing an object at the mouse position so I imagine I could also somehow use this information to determine the mouse's distance from the camera or to use it as an anchor point from which all momentum could be made. I just don't quite get how.

There is also a sub issue regarding my target. I am uncertain of how to make it keep the camera's rotation so as to ensure orbiting does not snap back to the forward vector.

 using UnityEngine;
 using System.Collections;
 
 public class cameramove : MonoBehaviour {
 
     public static cameramove Instance;
     public float speed = 30f;
     public float scrollspeed = 20f;
     public float Rotspeed = 50f;
 
     public Transform visual;
     public Transform target;
     public float scrollingClamp = 20.0f;
     public Vector3 Distance =  Vector3.forward * 20f;
     public float DistanceMin = 7f;
     public float DistanceMax = 15f;
     private Vector3 Position =  Vector3.zero;
     private Vector3 NewPosition =  Vector3.zero;
     public float panSpeed = 10f;
     public Vector3 mouseMove = Vector3.zero;
     public Vector3 moving = Vector3.zero;
     private Vector3 scrollingPos = Vector3.zero;
     public Vector3 Height = new Vector3(0.0f, 3f, 0.0f);
 
 
 
     public float YMinLimit = 5f;
     public float YMaxLimit = 80f;
     public float YHeight = 0f;
 
 
 
     public float X_MouseSensitivity = 5f;
     public float Y_MouseSensitivity = 5f;
 
 
 
     CharacterController cmove;
 
     // Use this for initialization
     void Start () {
          cmove = GetComponent<CharacterController>();
         //Distance = Mathf.Clamp(Distance,DistanceMin, DistanceMax);
     }
 
     void Awake(){
 
         Instance = this;
     }
     
     // Update is called once per frame
     void LateUpdate () {
 
 
 
         movingcamera();
 
         scrolling();
         turning ();
         raycasting();
     }
 
     void movingcamera(){
         //Moving the camera in relation to the world axis.
         moving = Vector3.zero;
 
         if(!Input.GetMouseButton(1))
         {
         if(Input.GetMouseButton(0))
         {
             moving = new Vector3(-Input.GetAxis("Mouse X") * panSpeed,  0,-Input.GetAxis("Mouse Y")* panSpeed);
             
                 target.transform.position = Camera.main.transform.position + Distance;
 
         }
         }
         moving = transform.TransformDirection(moving);
         moving *= speed;
 
         
         moving = new Vector3(moving.x, 0.0f , moving.z);
 
         cmove.Move(moving *Time.deltaTime);
 
     }
 
     void scrolling(){
 
         scrollingPos =  Vector3.zero;
         //this use to work on a pure input basis but now it doesn't. Now it slowly accumulates value!
         var deadZone = 0.01f;
         if(Input.GetAxis("Mouse ScrollWheel") < deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone){
         scrollingPos.y += Input.GetAxis("Mouse ScrollWheel") * scrollspeed;
 
 
         }
 
         
 
         cmove.Move(transform.forward *scrollingPos.y);
 
     }
     void turning(){
 
         Screen.lockCursor = false;
         Vector3 mouseMove =  Vector3.zero;
         if(Input.GetMouseButton(1)){
             Screen.lockCursor = true;
             mouseMove += new Vector3(Input.GetAxis("Mouse Y"),Input.GetAxis("Mouse X"),0.0f );
             cmove.transform.LookAt(target);
             mouseMove = transform.TransformDirection(mouseMove);
             cmove.transform.position += mouseMove * speed *Time.deltaTime;
 
         }
 
 
 
 
     }
 
     void raycasting()
     {
         RaycastHit vHit = new RaycastHit();
         Ray vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(vRay, out vHit, 1000))
         {
             Debug.Log("OK");
             visual.transform.position = vHit.point +Height;
         }
     }
 
 }
 



Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Detect Mouse in right side or left side For Player? 2 Answers

Unity Android Movement Problem 0 Answers

Camera for the player (Move with mouse to move camera) 1 Answer

How can I rotate/orbit the camera around an object? 1 Answer

Camera follow mouse, remain inside bounds? 0 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