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 Bincredible · Sep 21, 2016 at 02:08 PM · cameravector3charactercontroller

Getting character to face direction of camera

Hi, I have made a character controller script which makes the character move and also face the direction that it moves in, however I want to add a camera look script which makes the camera follow the player and will allow me to change the camera rotation with the mouse, and then the new forward of the character is the forward of the camera. I've tried lots of methods but I just can't get it right so that once the forward of the character is changed it moves in accordance to it's new forward vector. Here is my character controller script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     //Private variables
     private float _currentSpeed;
     private float _walkSpeed = 3.5f;
     private float _runSpeed = 5.0f;
     private float _rotateSpeed = 6.6f;
     private float _vvSpeed = 0;
     private float _jumpHeight = 2f;
     private float _doubleJumpHeight = 2.2f;
     private int _jumpNum = 0;
     private float _grav = 7.0f;
 
     //Public Variables
     public Vector3 movementVector = Vector3.zero;
     public CharacterController myController;
 
     void Start () {
         myController = transform.GetComponent<CharacterController>();
     }
 
     void Update () {
         if(Input.GetKey(KeyCode.LeftShift)){
             _currentSpeed = _runSpeed;
         }
         if(Input.GetKeyUp(KeyCode.LeftShift) || !Input.GetKey(KeyCode.LeftShift)){
             _currentSpeed = _walkSpeed;
         }
         movementVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         if(movementVector != Vector3.zero)
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementVector), Time.deltaTime * _rotateSpeed);
         if(myController.isGrounded){
             _jumpNum = 0;
             movementVector.y = 0;
             if(Input.GetKeyDown(KeyCode.Space)){
                 _jumpNum = 1;
                 _vvSpeed = _jumpHeight;    
             }
         }else{
             if(Input.GetKeyDown(KeyCode.Space) && _jumpNum == 1){
                 _vvSpeed = _doubleJumpHeight;
                 _jumpNum = 0;
             }    
         }
         _vvSpeed -= _grav * Time.deltaTime;
         movementVector.y = _vvSpeed;
         myController.Move(movementVector * _currentSpeed * Time.deltaTime);
     }
 
     //Accessed by BouncePad script
     public void Bounce(float amount){
         _vvSpeed = amount;    
     }
 
 }

I would just like to be pointed in the right direction. Thanks

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
0
Best Answer

Answer by Happy-Zomby · Sep 21, 2016 at 04:12 PM

Hi, I'm not sure I understand fully what you are looking for but I'm guessing what you want to do is make the camera a child object of your character then it will automatically follow your character around - and you can edit its position relative to the parent (your character) when using the mouse. If you look for camera as child you should find a lot of threads on this topic. Hope that helps,

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 Bincredible · Sep 21, 2016 at 06:49 PM 0
Share

When I attach the camera to the player, when I move left or right the camera will rotate to the same direction and it will look like I'm going forward when I'm holding the left arrow key. What I want is for the camera to follow the player but not rotate unless the mouse is used to rotate it. Then once the camera is rotated 90 degrees for example, the new forward of the character will be the same as the camera. And from then on the same rule applies where the character can move left and right without rotating the camera. It is very difficult to explain but it is basically the same concept as $$anonymous$$inecraft but in 3rd person.

avatar image Happy-Zomby · Sep 21, 2016 at 09:00 PM 0
Share

You need to rotate your camera around the character see here: http://answers.unity3d.com/questions/600577/camera-rotation-around-player-while-following.html

then for your new foward... you can add the vector 3 of the camera to the one of your player to create a projection point and then have your character rotate towards that projection point. hope that helps,

avatar image Bincredible Happy-Zomby · Sep 22, 2016 at 03:06 PM 0
Share

Ok that makes a lot of sense, I think this might be what I need to do. Thanks

avatar image
0

Answer by UNDERHILL · Sep 21, 2016 at 04:11 PM

https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

If the camera is on the player create an empty child on camera which extends in front of the player and target that with Transform.LookAt or use a negative multiplier on the fed vector ( whatever * -1.0f )

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 Bincredible · Sep 21, 2016 at 06:44 PM 0
Share

When I attach the camera to the player and make a child on the camera then make the camera look at the child using Transform.LookAt the camera looks down and shakes all over. Is this what you meant or have I done something wrong?

avatar image UNDERHILL Bincredible · Sep 22, 2016 at 02:37 AM 0
Share

There are a few ways to do this. I imagine that you have a character with a floating 'third person' camera above and behind them, yes?

Can you please be very very specific about your setup and what you are trying to accomplish? A screenshot would help a LOT here!

avatar image Bincredible UNDERHILL · Sep 22, 2016 at 03:03 PM 0
Share

Here is a screenshot of the player (the capsule) and the position of the camera behind it. Like I mentioned it is the same idea as $$anonymous$$inecraft that I'm trying to achieve but in a 3rd person type of view. alt text

screen-shot-2016-09-22-at-160009.png (144.9 kB)

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

86 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

Related Questions

Launching a Character controller using vectors 0 Answers

Object not moving in forward direction 1 Answer

Camera moves into Object instead of following 1 Answer

nullifying targetObject after a Smooth LookAt Transition 2 Answers

Is there a way to make a Character Controller stop instead of move over obstacles?? 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