• 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 SVGK · Apr 29, 2014 at 10:24 AM · cameravelocityrelative

Relative player controls velocity to camera rotation.

Hello, I've run into an issue, and I don't understand it at all, see, I'm trying to get the player to move relatively to the camera, which is an orbiting camera.

However, I need to it to ignore the height of the camera, and only use the general direction, however:

 public Transform cameraTransform;
 public float speed;
  
 public Vector3 forwardVel;
 public Vector3 horizontalVel;
  
 void Update()
 {
 float moveHorizontal = Input.GetAxis ("Horizontal");
 float moveVertical = Input.GetAxis ("Vertical");
  
 Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
  
 forwardVel = new Vector3(forwardVel.x, cameraTransform.transform.forward.y * speed * moveVertical, forwardVel.z);
 horizontalVel = new Vector3 (horizontalVel.x, cameraTransform.transform.right.y * speed * moveHorizontal, horizontalVel.z);
  
 rigidbody.velocity = forwardVel + horizontalVel;
 }

This code results in a bizzare bug, the player moves perfectly normally, if I just keep teh camera where it is or at a 180 degree turn, basically, turning the camera to face downwards on top of the player, or putting the camera at the right or left, these both make the character (which is a rigidbody) just slow down almost to a halt, I don't know what's causing this or how to fix it.

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
0

Answer by maddFrogg · Apr 29, 2014 at 10:28 AM

You cannot assign a single Vector3 component, but the whole Vector at once.

Your code should look like this

 Vector3 forwardVel = new Vector3 (forwardVel.x, cameraTransform.transform.forward.y * speed * moveVertical, forwardVel.z);
Comment
Add comment · Show 6 · 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 SVGK · Apr 29, 2014 at 10:46 AM 0
Share

Hm, it gives errors on forwardVel.x and forwardVel.z, that I'm using possibly unassigned fields.

Oddly enough, if I use the same type of code, but on assigning the horizontalVel, using forwardVel.x and z don't give any errors.

avatar image maddFrogg · Apr 29, 2014 at 11:30 AM 0
Share

Yo need to initialize forwardVel vector, or swap forwardVel.x and forwardVel.z with numbers.

avatar image SVGK · Apr 29, 2014 at 12:19 PM 0
Share

Initialize it in what way?, I've put it in the start() method, but I don't know what I'm meant to set it to, or what numbers to use in it's place, using 0 just makes the player float upwards and downwards in the air.

avatar image maddFrogg · Apr 29, 2014 at 12:39 PM 0
Share
 forwardVel = new Vector3(0, cameraTransform.transform.forward.y * speed * moveVertical, 0);

This is how you initialize a vector. If the variable is public you probably won't need to initialize it, as it assumes it's a Vector3.zero

I would need to see the code to know why it is floating around.

avatar image SVGK · Apr 29, 2014 at 01:22 PM 0
Share

Hm, right:

 public Camera theCamera;
 public Transform cameraTransform;
 public float speed;

 public Vector3 forwardVel;
 public Vector3 horizontalVel;

 void Update() 
 {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");

     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

     forwardVel = new Vector3(forwardVel.x, cameraTransform.transform.forward.y * speed * moveVertical, forwardVel.z);
     horizontalVel = new Vector3 (horizontalVel.x, cameraTransform.transform.right.y * speed * moveHorizontal, horizontalVel.z);
     
     rigidbody.velocity = forwardVel + horizontalVel;
         

There it is, just to recap, I'm trying to get this set up so that the player moves relative to the camera, but won't go slower or faster by rotating the camera, which it does, in fact, if you look down on the character, trying to go backwards lets you fly in the camera's direction.

Though right now, for some reason I can't think of, the player just goes right up and down into the air, and changing the camera still lets you fly upwards.

Show more comments
avatar image
0

Answer by pcbabu · Apr 29, 2014 at 12:29 PM

is should be cameraTransform.transform.right.x not cameraTransform.transform.right

Comment
Add comment · Show 1 · 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 SVGK · Apr 29, 2014 at 12:41 PM 0
Share

I don't think you know quite what's wrong, the line with .y is the broken one.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Can relative velocity be calculated from relative direction vector 3 Answers

Camera Rotate behind rotating ball 1 Answer

Camera Relative Movement 2 Answers

how to have an object positioned relative to the screen(like a button) 1 Answer

AddForce Relative to Rotation 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