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 PythonMaster18 · Jul 12, 2017 at 07:21 PM · camera-movementbeta

Move camera relative to where you're facing

Most questions want to rotate an object around an axis. But I need to rotate all three axis accordingly around to the rotation of the camera. This is due to the fact I want to mimic most first person shooters: if you press the key to go forward, you go forward relative to where you're facing not the axis. For example, if I turn my camera to the left 90 degrees and I press the forward button (up arrow), the camera moves forward... as if it didn't turn at all.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour {
 
     public float speed;
     public GameObject projectile;
     public GameObject hit_object;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.localPosition += Vector3.right * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += Vector3.left * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.DownArrow))
         {
             transform.position += Vector3.back * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.UpArrow))
         {
             transform.position += Vector3.forward * Time.deltaTime * speed;
         }
 
         if (Input.GetKeyDown(KeyCode.F))
         {
             GameObject bullet = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
             bullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, 2000));
         }
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             var pos_x = transform.position.x + Random.Range(-500F, 500F);
             var pos_z = transform.position.z + Random.Range(-500F, 500F);
             Instantiate(hit_object, new Vector3(pos_x, 400, transform.position.z + pos_z), Quaternion.identity);
         }
 
         transform.Rotate(0f, Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0f, Space.World);
         transform.Rotate(-Input.GetAxis("Vertical") * speed * Time.deltaTime, 0f, 0f, Space.Self);
     }
 }
 

alt text

In the above snippet, the camera is facing in the direction of the red arrow and thus expect it to move forward in that direction. Instead, it moves according the y-axis (blue arrow). How do I fix this problem?

capture.png (102.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by PythonMaster18 · Jul 12, 2017 at 08:50 PM

Now, I have solved my solution by using transform.right and transform.forward instead of Vector3. The relevant part of the code now looks likes this:

 if (Input.GetKey(KeyCode.RightArrow))
         {[link text][1]
             transform.localPosition += transform.right * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += -transform.right * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.DownArrow))
         {
             transform.position += -transform.forward * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.UpArrow))
         {
             transform.position += transform.forward * Time.deltaTime * speed;
         }

I have gotten my solution here as the answerer explained the difference of using transform and Vector3.

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

Answer by Vollmondum · Jul 12, 2017 at 07:37 PM

Vector3.forward moves your object in z = 0 direction, that's wrong script line. You want to move (direction.position - transform.position) Reference

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour {
 
     public float speed;
     public GameObject projectile;
     public GameObject hit_object;
     private Vector3 moveDirection = Vector3.zero;
     public Vector3 movementMultiplier;
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
         moveDirection = new Vector3(movementMultiplier.x, movementMultiplier.y, movementMultiplier.z);
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         if (Input.GetKey(KeyCode.RightArrow))
         {
             movementMultiplier.x = 1;
             movementMultiplier.z = 0;
             transform.position += moveDirection * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             movementMultiplier.x = -1;
             movementMultiplier.z = 0;
             transform.position += moveDirection * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.DownArrow))
         {
             movementMultiplier.x = 0;
             movementMultiplier.z = -1;
             transform.position += moveDirection * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.UpArrow))
         {
             movementMultiplier.x = 0;
             movementMultiplier.z = 1;
             transform.position += moveDirection * Time.deltaTime;
         }
 
         if (Input.GetKeyDown(KeyCode.F))
         {
             GameObject bullet = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
             bullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, 2000));
         }
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             var pos_x = transform.position.x + Random.Range(-500F, 500F);
             var pos_z = transform.position.z + Random.Range(-500F, 500F);
             Instantiate(hit_object, new Vector3(pos_x, 400, transform.position.z + pos_z), Quaternion.identity);
         }
     }
 }
 
Comment
Add comment · Show 9 · 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 PythonMaster18 · Jul 12, 2017 at 07:43 PM 0
Share

Could you add code to clarify? I am not quite sure what and where to add into my code.

avatar image Vollmondum PythonMaster18 · Jul 12, 2017 at 07:52 PM 0
Share

I'm not a C# lover, but the reference I gave you, initializes the direction you're facing. All you need to do is to add moveDirection to your transform.localPosition lines.

avatar image PythonMaster18 Vollmondum · Jul 12, 2017 at 08:02 PM 0
Share

So what relevant pieces of code from your reference is needed?

Show more comments

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

72 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

Related Questions

Z Rotation with Limits does not function properly. 0 Answers

Background objects get glitch while camera follow the player 0 Answers

Help with repositioning object to a random position in the FOV 1 Answer

Orthographic camera movement 0 Answers

Issue with my camera controls 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