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 CoolOlivie · Sep 30, 2018 at 07:52 PM · camera-lookroll a balladdforce movement

How to change addForce direction depending on camera direction

Hey so I was making a game where the player is almost the same where it is in the 'roll a ball tutorial' as it is a ball the rolls around when the player does WASD or arrow keys, I also added a script to the camera to make it orbit around the player when you move the mouse. But the trouble is that if I move my mouse & press W, instead of the ball rolling away from the camera, it rolls in the same direction as before. If possible I would prefer to keep it as the ball 'rolls' around rather than just translating is it has that 'inertia' & behaves just like a real ball would. Here is the code for the player movement:

 public static float speed;
 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);
 } 

& here is the code for the camera:

 public GameObject player;
 public Transform plyrTransform;
 public Transform camTransform;
 public Camera cam;
 private float distance = 10.0f;
 private float currentX = 0.0f;
 private float currentY = 0.0f;
 public float sensitivity = 1.0f;
 private float sensitivityX = 0.0f;
 private float sensitivityY = 0.0f;

 void Start () {
     camTransform = transform;
     sensitivityX = sensitivity;
     sensitivityY = sensitivity * 4;
 }

 void Update () {
     currentX += Input.GetAxis ("Mouse X") * sensitivityX;
     currentY += Input.GetAxis ("Mouse Y") * sensitivityY;
     distance += Input.GetAxis ("Mouse ScrollWheel");
 }

 void LateUpdate () {
     Vector3 dir = new Vector3 (0, 0, -distance);
     Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
     camTransform.position = plyrTransform.position + rotation * dir;
     transform.LookAt (player.transform);
 }

P.S, sorry if some of the code is unnecessary & messy, I am new to C# & follow a lot of tutorials.

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 hexagonius · Sep 30, 2018 at 08:08 PM

the movement code uses the axis as x and z input, which is in world space. to get a camera relative direction, use TransformDirection on the camera transform for that vector, save the magnitude, zero out y and set the saved magnitude again.

Comment
Add comment · Show 3 · 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 CoolOlivie · Sep 30, 2018 at 08:21 PM 0
Share

@hexagonius Thanks for the reply, but I am afraid I do not understand completely as I am new to C#

avatar image hexagonius CoolOlivie · Sep 30, 2018 at 09:52 PM 0
Share

then you should try to learn C# first, as you will probably encounter your next problem right after this one is solved. and comment next time if you're not actually answering.

avatar image CoolOlivie hexagonius · Sep 30, 2018 at 09:54 PM 0
Share

$$anonymous$$ thanx anyway, I think I can 'sort of' understand it I am just a bit confused about magnitude but I will search it up on the documentation.

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

91 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

Related Questions

Restrict Scaling of Object due to camera position and angle. 1 Answer

Keyboard Camera control script question 1 Answer

Camera Script problems 1 Answer

How to identify list of Objects in Camera view 1 Answer

How can I get an invisible object to cast a shadow? 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