Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zeetoh · Dec 23, 2017 at 08:34 PM · cameracamera-movementcamera rotatecamera-lookcamera rotation

Camera movement according to rotation and actual position RTS

Hi guys i'm doing and RTS TD game. My camera can rotate all around the field. My camera has a rotation on X of 65 degrees from start; the player can see the whole field from that view. Since my player can rotate at will, i want to let him move up, down, left and right after he rotated, and my plan is to keep W A S D keys to move up, left, down from the new point of view. My issue is W and S in Space.World won't let the player move after rotating his camera (it ignores his actual rotatio), so i tried using Space.Self and Vector3.forward, Vector3.up and Vector3.back, Vector3.down but all of them won't have the results i want, because .forward and .back makes the player zoom in and out respectively. And .up and .down actually kinda work, but they make the player zoom in and out too.

So any ideas to let the player keep his WASD movement after used the rotatios feature?

Right now i letft W and S on Space.World, so i can move around without rotation. And A and D can move perfectly before and after rotating.

 using UnityEngine;
 
 public class CameraController : MonoBehaviour {
 
 
     public float panSpeed = 30f;
     public float panBorderThickness = 10f;
     public float scrollSpeed = 20f;
     public Vector2 panLimit;
     public float minY;
     public float maxY;
 
     Vector3 lastMousePosition;
     float rotateSpeed = 5f;
 
 
 
 
     void Update()
     {
 
         
 
         if (GameManager.GameIsOver)
         {
             this.enabled = false;
             return;
         }
         if (Input.GetKey("w") || Input.mousePosition.y >= Screen.height - panBorderThickness)
         {
             transform.Translate(Vector3.forward * panSpeed * Time.deltaTime, Space.World);
         }
         if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
         {
             transform.Translate(Vector3.back * panSpeed * Time.deltaTime, Space.World);
         }
         if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)
         {
             transform.Translate(Vector3.right * panSpeed * Time.deltaTime, Space.Self);
         }
         if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
         {
             transform.Translate(Vector3.left * panSpeed * Time.deltaTime, Space.Self);
         }
         if (Input.GetKey("q"))
         {
             transform.Rotate(Vector3.up * Time.deltaTime * -scrollSpeed*4, Space.World);
         }
         if (Input.GetKey("e"))
         {
             transform.Rotate(Vector3.down * Time.deltaTime * -scrollSpeed*4, Space.World);
         }
         // Middle click mouse rotation
         if (Input.GetMouseButton(2))
         {
             // if the game window is separate from the editor window and the editor
             // window is active then you go to right-click on the game window the
             // rotation jumps if  we don't ignore the mouseDelta for that frame.
             Vector3 mouseDelta;
             if (lastMousePosition.x >= 0
                 && lastMousePosition.y >= 0
                 && lastMousePosition.x <= Screen.width
                 && lastMousePosition.y <= Screen.height)
                 mouseDelta = Input.mousePosition - lastMousePosition;
             else
                 mouseDelta = Vector3.zero;
 
             Vector3 rotation = Vector3.up * Time.deltaTime * rotateSpeed * mouseDelta.x;
             rotation += Vector3.left * Time.deltaTime * rotateSpeed * mouseDelta.y;
             transform.Rotate(rotation, Space.Self);
 
             // Make sure z rotation stays locked
             rotation = transform.rotation.eulerAngles;
             rotation.z = 0;
             transform.rotation = Quaternion.Euler(rotation);
         }
         lastMousePosition = Input.mousePosition;
 
         Vector3 pos = transform.position;
         pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x + 90);
         pos.z = Mathf.Clamp(pos.z, -panLimit.y, panLimit.y);
         pos.y = Mathf.Clamp(pos.y, minY, maxY);
         float scroll = Input.GetAxis("Mouse ScrollWheel");
         pos.y -= scroll * 100 * scrollSpeed * Time.deltaTime;
        
         transform.position = pos; 
 
     }
 }

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 DreadKyller · Dec 23, 2017 at 10:05 PM

Instead of using the directions provided by Vector3 ( Vector3.up, Vector3.left, etc) use the up, right, and forward properties of the transform ( transform.forward, transform.up, transform.right) to get backwads dow and left just negate it:

Forward = transform.forward
Backwards = -transform.forward
Up = transform.up
Down = -transform.up
Right = transform.right
Left = -transform.right

These directions represent the local direction vectors for the transform, in world space.

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

118 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

Related Questions

Camera gets stuck when cursor is locked 0 Answers

camera zoom 1 Answer

How to allow camera to complete an upside down rotation while using LookAt() 0 Answers

Camera movement and gun follow in unity 1 Answer

Camera follow a sphere which look at another object on the ground 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