Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 GetColor · Jul 14, 2015 at 10:31 AM · cameramovement

Moving towards where the camera is looking

I'm trying to make a "marble blast" style game, where you control a ball.

I currently have a camera that orbits around a ball, and the camera is controlled by the mouse. I added basic movement to the ball using addforce, but now I'm trying to modify my script so that when you press W (or forward on the stick) you move forward in the direction the camera is facing, and if you press right, the ball moves to the right of the camera etc. etc.

I'm having issues modifying my code to do this and move relative to where the camera is facing. Any help is greatly appreciated!

I have looked through similar threads, but all of them use translation for the character, and I can't seem to figure it out for adding physics forces in the camera's direction.

Below is the code I'm trying to modify:

 using UnityEngine;
 using System.Collections;
 
 public class s_PlayerMovement : MonoBehaviour {
 
     public float speed;
     public Camera datCamera;
     private Rigidbody rb;
     
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         
         rb.AddForce (movement * speed);
     }
 }

Comment
Add comment · Show 2
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 Veldars · Jul 14, 2015 at 11:59 AM 1
Share

Hi, I don't have time to test my answer but I think you can try somethings like :

          float moveHorizontal = Input.GetAxis ("Horizontal");
          float moveVertical = Input.GetAxis ("Vertical");
          
          Vector3 h$$anonymous$$ovement = moveHorizontal * datCamera.transform.right;
          Vector3 v$$anonymous$$ovement = moveVertical * datCamera.transform.forward;
 
           rb.AddForce ((h$$anonymous$$ovement + v$$anonymous$$ovement) * speed);

If that doesn't works the key point for your search is :

"datCamera.transform.forward" and "datCamera.transform.right" which are the camera forward vector and right vector.

I hope this is helpfull....

avatar image GetColor · Jul 15, 2015 at 07:32 AM 0
Share

It works perfectly! Thank you so much :D

Looking at the code, it makes perfect sense, I just couldn't for the life of me figure that out.

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

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

How can I produce the typical FPS mouse steering effect? 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

How to use Android Accelerometer(Left and right tilt) to turn a sphere object and camera in that direction? 1 Answer

Constrain axis of isometric camera ("vertical" only) 0 Answers

FPS camera movement breaks GameObject 2 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