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 unity_9447unni9447 · May 06, 2020 at 01:08 PM · third person controllerrelative rotationrelative movement

Can anybody modify my script so that my player could move related to camera

game developer who is developing a third person shooter game. I made most of the part in the movement script. But I am not able to modify my script so that my player is not moving relative to the camera. The below shown script is the one I made. Can anybody modify the script so that the player moves relative to the camera,Hello. I am having trouble making a third person shooter game. I create a script in which the character look towards the direction it should be moving, and the movement is completely using animation. I would like to modify the below given script so that the player always moves in the direction where the camera is facing. It would be good if anybody could help me. I am just an intermediate to game dev

public class PlayerMovement : MonoBehaviour {

 private Animator animator;
 public Transform CamLoc;
 private Vector3 localCam;

 [SerializeField]
 private float turnSpeed = 5f;

 private void Awake()
 {
     animator = GetComponentInChildren<Animator>();
 }

 private void Update()
 {
     var horizontal = Input.GetAxis("Horizontal");
     var vertical = Input.GetAxis("Vertical");
     var movement = new Vector3(horizontal, 0, vertical);
     animator.SetFloat("Mag", movement.magnitude);

     if (movement.magnitude > 0)
     {
         Quaternion newDirection = Quaternion.LookRotation(movement);

         transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSpeed);
         
     }
 }

},

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 Tario · May 06, 2020 at 02:44 PM

 public Camera cam; // a reference to your camera
 var verticalMovement = cam.transform.forward; // The forward vector of your camera
 verticalMovement.y = 0; // You don't want the character to be moving up or down based on the camera's direction, right?
 var movement = horizontal * cam.transform.left + vertical * verticalMovement;

The input at the moment is being applied straight to the worldspace coordinates of the object. You want to apply it to the relative left and forward vectors of the object. Your character will now move -1 to 1 units left of the camera and -1 to 1 units forward relative to the camera each frame.

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 Tario · May 06, 2020 at 02:44 PM 0
Share

if the horizontal axis is inverted, swap out transform.left for transform.right.

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

126 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 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

Simple Third Person Camera-Relative Movement with WASD - kind of working but need a fix 2 Answers

3rd person character movement relative to camera 1 Answer

AI strafe animations 1 Answer

Third person controller & animation.. NEED HELP 0 Answers

How do I ''Apply'' the animations to characters? 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