Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 IonoI · May 26, 2015 at 12:45 PM · movementaddforcecontrolscamera followcamera movement

Player movement relative to the camera

I'm new to unity so I wanted to start by making simple controls for a player. The player is a ball that rolls using physics, so i'm using addForce instead of transform.position.

Whats the best way to control the player with the camera like this?:

W = add force horizontally away from the camera

S = add force horizontally towards the camera

A,D = add force left or right

Space = jump

Then I think I can figure out how to make the camera orbit the player and follow.

I think I need a way to get the direction from the camera to the player(with raycast?) then to push the player at 0, 0+180, 0+90, or 0-90 degrees of that direction depending on which button is down. I couldn't find any tutorials on how to do that.

From tutorials and the manual I only found out how to make it move relative to the scene, but that wouldnt really work if the camera can rotate around the player.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · May 26, 2015 at 06:34 PM

One way to approach it is to consider what direction means what; more specifically, is the camera's right unconditionally "right" and is the ground's forward unconditionally "forward"?

That said, here's a general approach I've often taken on this:

 // C#
 
 // character and camera Transform variables may or may not be necessary
 Transform player;
 Transform cam;
 
 // This establishes basic controls relative to the camera
 Vector3 controlRight = Vector3.Cross(cam.up, cam.forward);
 Vector3 controlForward = Vector3.Cross(cam.right, Vector3.up);
 
 // Apply movement to control input
 Vector3 controlInput = (controlRight * Input.GetAxis("Horizontal")) + (controlForward * Input.GetAxis("Vertical"));

With some tweaks and personalization, this may serve as a reasonable starting point.

Edit: In case you might be wondering why I bothered with controlRight (since cross(cam.up, cam.forward) is simply cam.right), it's for the sake of having a point to branch off from. The controls don't have to exclusively be based on the camera itself, so thinking ahead to separate the controls into two pieces can save a lot of headache later.

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
0

Answer by HenryStrattonFW · May 26, 2015 at 01:03 PM

Well it's very similar to do it relative to the camera, the only thing that will change is what value you are applying (I'm assuming that relative to the scene you've just been adding things like Vector3.Left, Vector3.Right or the equivelent new Vector3(1.0f, 0.0f, 0.0f) etc)

To move relative to the camera you can simply use some of the helper values of the Transform component that exists as part of every gameObject (even the camera).

So for example right, relative to the main camera, would be.

Camera.mainCamera.transform.right;

You can use the inverse of this for the camera left, the forward value (instead of right) and its negative for away from and towards the camera respectively, and its up and down for your jumping.

Keep in mind however that if your camera is pitched up or down, then the forward vector will not be level with the "floor" of the world.

Hope this helps as a point in the right direction.

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
0

Answer by IonoI · May 27, 2015 at 11:38 AM

Thanks, I almost got it to work like this:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     public GameObject player;
     public GameObject cam;
 
     public Transform playerT;
     public Transform camT;
 
 
     private Vector3 offset = Vector3.zero;
 
     void Start () {
         offset = camT.position - playerT.position;
     }
 
     void LateUpdate () {
         camT.LookAt(playerT);//allways look at player
 
         if ( Input.GetMouseButton( 1 ) ) {//right click is down
             camT.RotateAround(playerT.position, Vector3.up, Input.GetAxis("Mouse X"));//look horizontally
             camT.RotateAround(playerT.position, Vector3.left, Input.GetAxis("Mouse Y"));//look vertically
         }
         camT.position = playerT.position + offset;//keep same distance from player
     }
 }

The only problem left is that the offset interferes with the camera rotating. The offset is meant to keep the camera a certain distance away from the player, but now it also controls the exact position relative to the player instead of just the distance. I want to have a zoom distance variable that can be controlled by the mouse wheel.

I think it's a simple solution that i'll eventually figure out to somehow update the offset every time the camera is rotated, but does anyone want to give me a hint? :D

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 JustinTheSwift · Feb 11, 2017 at 12:34 AM 0
Share

I'm having the same problem as far as the camera rotation goes. Watch my question and maybe you'll get an answer there (I think I've gotten a little farther on it that you have). http://answers.unity3d.com/questions/1311483/camera-that-both-orbits-and-follows.html

avatar image
0

Answer by iuripujol · Jun 23, 2018 at 04:33 PM

Vector3 move = Quaternion.Euler(0, camera.eulerAngles.y,0) * new Vector3(h,0,v).normalized;

Vector3 dir = transform.InverseTransformDirection(move); float Turn = Mathf.Atan2(dir.x,dir.z); transform.Rotate(0, Turn * someSpeed,0);

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddForce/ForceMode.Impulse 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Rotate camera a set amount of degrees around Player 0 Answers

Smooth camera on moving platform 1 Answer

Cinemachine & Playmaker 3rd person zoom 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