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 Lordician · Sep 08, 2014 at 03:29 PM · camera movement

Issue with RTS-like camera script

I am trying to create a simple RTS-like camera movement script using C# and i ran into a problem which i cannot understand. I can move the camera on both the X and Z axis, but for some reason, moving it on the Y-Axis with the scroll wheel doesn't work.

This is the code i use:

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovement : MonoBehaviour {
 
     public float cameraMovementSpeed = 2.0f;
     public float cameraZoomSpeed = 20.0f;
     public float cameraMinHeight = 10.0f;
     public float cameraMaxHeight = 40.0f;
     public bool mouseDeadZoneScrolling = true;
     public float mouseDeadZoneDistance = 100.0f;
     public float xMoveValue = 0.0f;
     public float yMoveValue = 0.0f;
     public float zMoveValue = 0.0f;
 
     // Use this for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         float xAxisValue = Input.GetAxis("Horizontal");
         float yAxisValue = Input.GetAxis("Mouse ScrollWheel");
         float zAxisValue = Input.GetAxis("Vertical");
         xMoveValue = xAxisValue * cameraMovementSpeed;
         yMoveValue = -yAxisValue * cameraZoomSpeed;
         zMoveValue = zAxisValue * cameraMovementSpeed;
 
         if (Input.mousePresent)
         {
             if (mouseDeadZoneScrolling)
             {
                 Vector3 mousePos = Input.mousePosition;
                 print("Mousepos: " + mousePos + " | Scrolling: " + yAxisValue + " = " + yMoveValue);
                 if (mousePos.x <= mouseDeadZoneDistance)
                 {
                     float distanceFromDeadzone = mouseDeadZoneDistance - mousePos.x;
                     float deadZoneModifier = (1/mouseDeadZoneDistance) * distanceFromDeadzone;
                     xMoveValue = -cameraMovementSpeed * deadZoneModifier;
                 }
                 else if (mousePos.x >= (Screen.width - mouseDeadZoneDistance))
                 {
                     float distanceFromDeadzone = mousePos.x - (Screen.width - mouseDeadZoneDistance);
                     float deadZoneModifier = (1/mouseDeadZoneDistance) * distanceFromDeadzone;
                     xMoveValue = cameraMovementSpeed * deadZoneModifier;
                 }
 
                 if (mousePos.y <= mouseDeadZoneDistance)
                 {
                     float distanceFromDeadzone = mouseDeadZoneDistance - mousePos.y;
                     float deadZoneModifier = (1/mouseDeadZoneDistance) * distanceFromDeadzone;
                     zMoveValue = -cameraMovementSpeed * deadZoneModifier;
                 }
                 else if (mousePos.y >= (Screen.height - mouseDeadZoneDistance))
                 {
                     float distanceFromDeadzone = mousePos.y - (Screen.height - mouseDeadZoneDistance);
                     float deadZoneModifier = (1/mouseDeadZoneDistance) * distanceFromDeadzone;
                     zMoveValue = cameraMovementSpeed * deadZoneModifier;
                 }
             }
         }
 
         if(Camera.current != null)
         {
             Camera.current.transform.Translate(new Vector3(xMoveValue, yMoveValue, zMoveValue), Space.World);
         }
     }
 }
 


Sorry for the large code block, but someone might actually find the rest of the camera script interesting perhaps. As you might see, i use Input.GetAxis("Mouse ScrollWheel") to see if there is any scrolling and later on, when i print out the value, it does give a value sometimes (mostly 0, but it gives -0.2, -0.1, 0.1 or 0.2 when i scroll). It even shows that yMoveValue gets updated accordingly, but the camera just won't move on the Y-Axis. This is all i currently have in the project, so there are no joints or anything on the camera, it is just the default camera with an FOV of 90, set up to look straight down from a height of 30. So the camera transform looks like this: Position: X[0] Y[30] Z[0] Rotation: X[90] Y[0] Z[0] Scale: X[1] Y[1] Z[1]

This should be pretty simple, but for some reason it doesn't work like this. Are there any settings in the input settings i have to change like the gravity of the scrollwheel or anything?

If you got any remarks for improving the question/post, please tell me too as i am probably doing this very awkwardly as this is my first question i ask here.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by orb · Sep 08, 2014 at 04:23 PM

Reference the actual camera rather than Camera.current.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[C#] Smooth Camera Movement Restrictions 0 Answers

Creating a file that records the player position during a game in xyz coordinates 0 Answers

Mouse Look Help 2 Answers

Camera that both orbits and follows 1 Answer

How to rotate and move the camera with correct orientation,How to move camera after it gets rotated 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