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 /
This question was closed Mar 10, 2015 at 09:55 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by erlo68 · Mar 07, 2015 at 06:13 PM · c#cameramovementcharactercontrollerrelative

Character movement relative to the camera

Hi,

I got a Camera with a MouseOrbit-Script attached, and a Character with a absic controll script, i need help with making my character move relative to the camera, so if he moves forward he walks away from the camera and if he walks back he moves towards it. Even if i turn the camera. So that i would walk a full circle around an objct that i am locked on just by going left or right.

In the end it should behave like in Skyrim that i can controll the mouse and the player seperately but in relation to each other.

So i prepared some code to choose which direction he should face determined on which key is pressed, and the unity basic move script:

 if(Input.GetKey ("w") || Input.GetKey ("a") || Input.GetKey ("s") || Input.GetKey ("d"))
         {
             _characterState = CharacterState.Walking;
             if (Input.GetKey ("w")) 
             {
                            //go forward
             } else if (Input.GetKey ("s")) 
             {
                            //go backward
             } else if (Input.GetKey ("d")) 
             {
                            //go right
             }else if (Input.GetKey ("a")) 
             {
                            //go left
             }
         }
 
         if (_characterController.isGrounded) 
         {
             moveDirection = new Vector3(-Input.GetAxis("Horizontal"),0,-Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
 
             if(_characterState == CharacterState.Walking)
             {
                 moveDirection *= walkSpeed;
             }else if(_characterState == CharacterState.Running)
             {
                 moveDirection *= runSpeed;
             }
             if(Input.GetButton("Jump"))
             {
                 moveDirection.y = jumpSpeed;
             }
         }
         moveDirection.y -= gravity * Time.deltaTime;
         _characterController.Move (moveDirection * Time.deltaTime);
 

I tried to put the movement in a method so i could acces it if the pressed key was determined and send the information which direction it should go this way, but this somehow interfered with my animations and i don't know why because it just checks which animation is active right now and thats it... or it was messing with my CharacterStates... somehow...

Thanks in advance :3

PS: Sry for my probably bad english ^^"

As requested here are some Screenshots, even if ther isn't much to show:

Screenshot1

Screenshot2

Comment
Add comment · Show 3
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 Simple_Zomb · Mar 07, 2015 at 08:12 PM 0
Share

So basically you are trying to do 3rd person of I get it correctly? Im not really sure what you mean. Could you post some Screenshots maybe?

As far as I understand it you just want to get 3rd person.

So you dont even need a Script you can just make the Camera a child of the Character and that should do it.

Hope i could help :) -Simple_zomb

avatar image erlo68 Simple_Zomb · Mar 08, 2015 at 12:18 PM 0
Share

And that's exactly not what i want... sure it is supposed to be 3rd person, but i want to controll the Camera with the $$anonymous$$ouse (should have mentioned that i guess) and the character with w,a,s,d... so they are controlled seperately but in relation to each other just like in Skyrim.

You want some screenshots? gonna add them to the main post ^^

avatar image meat5000 ♦ · Mar 10, 2015 at 09:54 PM 0
Share

$$anonymous$$udos on solving your problem. I'm late on this but I'll post it anyway.

I found Camera relative very easy using Turn left/turn right animations and simply calculating the difference between Vector2 position of the joystick and player facing direction x/z in a vector2 with respect to the angle from the camera axis.

I wrote a pigeon-english tips based 'guide'

http://answers.unity3d.com/questions/497353/camera-relative-movement-1.html

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by erlo68 · Mar 10, 2015 at 09:48 PM

THIS WORKS FINE WITH A CHARACTER-CONTROLLER AND A CAMERA WITH UNITY'S MOUSE-ORBIT SCRIPT TO MAKE A THIRD PERSON GAME WHERE I CONTROLL THE CAMERA WITH MY MOUSE AND THE CHARACTER SEPERATELY WITH KEYBOARD OR A GAMEPAD!!!! (For Perople who are searching exactly that!)

Obligatory example video ---> Click Me

So, i solved the problem on my own:

I made the character walk in the right direction and relative to the camera using another movement-script i found:

 if (_characterController.isGrounded) 
         {
             Vector3 forward = _camera.transform.TransformDirection(Vector3.forward);
             forward.y = 0;
             forward = forward.normalized;
             Vector3 right  = new Vector3(forward.z, 0, -forward.x);
             float h = Input.GetAxis("Horizontal");
             float v =Input.GetAxis("Vertical");
             
             moveDirection  = (h * right  + v * forward);
 
             if(_characterState == CharacterState.Walking)
             {
                 moveDirection *= walkSpeed;
             }else if(_characterState == CharacterState.Running)
             {
                 moveDirection *= runSpeed;
             }
             
             if (Input.GetButton ("Jump")) 
             {
                 moveDirection.y = jumpSpeed;
             }
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
         _characterController.Move (moveDirection * Time.deltaTime);





But like that he would still always look in the same direction, so i made a new Method that turns him smoothly in the direction i want him to look, by sending a Vector3 containing that data and the method sets this in relation to the camera:

 void getRotation (Vector3 toRotation)
     {
         Vector3 relativePos = _camera.transform.TransformDirection(toRotation);
         relativePos.y = 0.0f;
         Quaternion rotation = Quaternion.LookRotation(relativePos);
         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
     }






I determine the pressed key pretty rudimentary but it works like i want it to so it's ok for me, and he can walk in 8 axes:

     if (Input.GetKey ("w") && Input.GetKey ("d")) 
                 {
                     getRotation(new Vector3(-1f, 0f, -1f));
                 }else if (Input.GetKey ("w") && Input.GetKey ("a")) 
                 {
                     getRotation(new Vector3(1f, 0f, -1f));
                 }else if (Input.GetKey ("s") && Input.GetKey ("d")) 
                 {
                     getRotation(new Vector3(-1f, 0f, 1f));
                 }else if (Input.GetKey ("s") && Input.GetKey ("a")) 
                 {
                     getRotation(new Vector3(1f, 0f, 1f));
                 }else if (Input.GetKey ("w"))
                 {
                     getRotation(new Vector3(0f, 0f, -1f));
                 } else if (Input.GetKey ("s")) 
                 {
                     getRotation(new Vector3(0f, 0f, 1f));
                 } else if (Input.GetKey ("d")) 
                 {
                     getRotation(new Vector3(-1f, 0f, 0f));
                 }else if (Input.GetKey ("a")) 
                 {
                     getRotation(new Vector3(1f, 0f, 0f));
                 }
 
  
 

Comment
Add comment · Show 7 · 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 Nyubie · May 06, 2016 at 11:04 AM 0
Share

may i ask if i can see the whole script for this? i am having trouble with my movements with camera

avatar image erlo68 · May 06, 2016 at 01:26 PM 0
Share

Here you go..

Camera Script:

 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var y$$anonymous$$inLimit = -20;
 var y$$anonymous$$axLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 
 @script AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit")
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     // $$anonymous$$ake the rigid body not change rotation
        if (GetComponent.<Rigidbody>())
         GetComponent.<Rigidbody>().freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target) {
         x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02;
          
          y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
                 
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
     }
 }
 
 static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
 }
avatar image Nyubie erlo68 · May 06, 2016 at 01:33 PM 0
Share

i meant about the movement, but thanks for camera too.

avatar image erlo68 Nyubie · May 06, 2016 at 01:38 PM 1
Share

Ok, sorry had some trouble uplaoding the movement because its too long... so heres a download-link for the .cs

http://www.file-upload.net/download-11551658/Third_Person_Controller.cs.html

PS: Ignore the animation segments they are far from finished and should be deleted if not needed!

Show more comments
avatar image Nyubie erlo68 · May 06, 2016 at 01:46 PM 0
Share

thanks for the help, i will try this tomorrow. Godbless.

avatar image rbisso · Nov 24, 2019 at 02:37 AM 0
Share

Great answer, this is exactly what I was looking for. Thank you so, much!

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

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

Move player relative to camera 0 Answers

How do I make my character lean to the sides? 0 Answers

How to relative movement system based on camera direction 1 Answer

In-Air movement relative to camera 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