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 Xephex · May 26, 2014 at 12:29 PM · cameracamera movementrelativerelative movement

How to move a tumbling player relative to the camera?

I have been googling this a lot and have found lots of information. However that information doesn't seem to work in my case. I have a tumbling box that is moved around the scene currently with 'rigidbody.velocity.z=speed' etc. However this is an issue when the player moves the player as all the controls become inverted as the controls are working from world directions. How can I make it so that the when I press the 'D' key, the player will always appear to go right from the cameras perspective.

Here is the code for the Camera follow: var target : Transform; var damp : float = 5; var distance : float = 20;

 function Update(){
  
     damp = Mathf.Clamp(damp, 0.01, 10); 
     var pCam = transform.position;
     var pTarget = target.position;
     var diff: Vector3 = pTarget - pCam; 
     var dist = diff.magnitude; 
 
     if (Mathf.Abs(diff.y) < 0.7*distance){
        diff.y = 0; 
 
     } 
     if (dist>distance){ 
        diff *= 1-distance/dist; 
       
        transform.position = pCam + diff * Time.deltaTime/damp;
     }
     transform.LookAt(pTarget);
 }

And the script for the player to move is here. As you can see I have tried using 'cam.TransformDirection(Vector3.right);' and 'Camera.main.transform.transform.back*speed;' Neither result in any movement. I've remove any irrelevant parts to make it more clear:

 #pragma strict
 var speed : float = 500;
 var cam : Transform;
 var camRelativeRight : Vector3 = cam.TransformDirection(Vector3.right);
 var camRelativeLeft : Vector3 = cam.TransformDirection(Vector3.left);
 var camRelativeForward : Vector3 = cam.TransformDirection(Vector3.forward);
 var camRelativeBack : Vector3 = cam.TransformDirection(Vector3.back);
 
 function Update () { 
     if(Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.LeftShift)) { 
         camRelativeForward*speed;
     }
         
     if(Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.LeftShift)) { 
         Camera.main.transform.back*speed;    
     }
         
     if(Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.LeftShift)) { 
             Camera.main.transform.right*speed;
     }
         
     if(Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftShift)) {
             Camera.main.transform.left*speed;
     }
         
 }

The player is not a parent of the camera. This would result in the camera tumbling around as the player game object tumbles. I wasn't sure if this could be the issue.

Thank you in advance for any help :).

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 Loius · Jun 21, 2014 at 08:10 PM

You need to transform your input vector from world space into camera space.

The function for this is either Transform.TransformDirection or Transform.InverseTransformDirection (I always forget).

Vector3 relativeInput = Camera.main.transform.TransformDirection(worldInput);

You have to do this in Update, since the camera is moving all the time. The way you have it is only checking the camera's orientation one time at the start.

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 siaran · May 26, 2014 at 02:28 PM

I'm not seeing any code that updates your player's position. In your player update script try changing camRelativeForward * speed; to transfrom.position += camRelativeForward*speed * Time.deltaTime; (and also for the other directions)

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 Xephex · May 27, 2014 at 02:28 PM 0
Share

I'm having issues with the camRelativeForward variable. It throws this error: NullReferenceException: Object reference not set to an instance of an object playerControl..ctor () (at Assets/playerControl.js:5) (Line 5 in the full script is the camRelativeRight variable. The first of the relative direction variables) I understand that this could have meant that I hadn't set the target object. However the target is correctly set to the player. If it wasn't the camera wouldn't be able to follow the player through the Transform.LookAt thing. The camera works perfectly it's just that I can't get controls relative to the camera working. Thanks for your help Siaran. I've tried it and it doesn't work. But I think it is because the camRelativeForward variable is broken. Not because your answer is incorrect.

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 to relative movement system based on camera direction 1 Answer

C# AddForce Relative To Camera Left And Right Movement 1 Answer

How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers

Camera Relative Movement 2 Answers

Camera relative rigidbody third person player movement 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