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 /
  • Help Room /
avatar image
0
Question by jaredmpersonal · Aug 18, 2020 at 08:22 PM · camerarigidbodycamera-movementcharacter movementmoveposition

How Use rigidbody.MovePosistion With A Player Controlled Moving Camera

Hey guys I am having trouble with some third person movement I am trying to implement. I am using the rigidbody.MovePosition function. In my game that player can use the WASD keys to move all directions and can use the keys q and e to rotate the camera around a focal point I have setup. I am trying to make so that my player can move in the same direction as the camera as well as moving diagonally (Holding the up key and the right key at the same time to go at an angle). I have been able to do these things seperate but can't seem to figure out how I would use the MovePosistion to do them both together. Here is the code for my player controller.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float horizontalInput;
     public float verticalInput;
 
     [SerializeField] float speed = 1;
 
     public Vector3 forceVector;
     public Rigidbody playerRb;
 
     public GameObject focalPoint;
 
     // Start is called before the first frame update
     void Start()
     {
         focalPoint = GameObject.Find("FocalPoint");
     }
 
     // Update is called once per frame
     void Update()
     {
         horizontalInput = Input.GetAxisRaw("Horizontal");
         verticalInput = Input.GetAxisRaw("Vertical");
 
         // Code that tries to make so that you can walk diagnol while also having the camera follow you
         /*
         forceVector = new Vector3(horizontalInput, 0, verticalInput);
         forceVector *= speed * Time.fixedDeltaTime;
 
         forceVector = Vector3.Scale(forceVector, focalPoint.transform.right);
         forceVector = Vector3.Scale(forceVector, focalPoint.transform.forward);
         */
 
     }
 
     void FixedUpdate()
     {
         playerRb.MovePosition(transform.position + forceVector);
         if (horizontalInput != 0)
         {
             playerRb.MovePosition(transform.position + (focalPoint.transform.right * horizontalInput * speed).normalized);
         }
 
         if (verticalInput != 0)
         {
             playerRb.MovePosition(transform.position + (focalPoint.transform.forward * verticalInput * speed).normalized);
         }
         
         // playerRb.AddForce(transform.right * speed * horizontalInput);
         // playerRb.AddForce(transform.forward * speed * verticalInput);
     }
 }

As shown in this code I have tried a few things and haven't been able to get it to work. The commented code was me trying to come up with the Vector Math to solve the issue. I have the two MovePosistions separate in the FixedUpdate(). This doesn't work for me because you can't walk diagonal you can only walk one direction at a time. I am trying to come up with some vector math that can combine the players input and the direction of the camera.

Here is my camera controller class

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour
 {
 
     public GameObject player;
     public Vector3 offset;
 
     public float rotationSpeed;
 
     public float horizontalCamMovement;
     public float verticalCamMovement;
 
     // Start is called before the first frame update
     void Start()
     {
         offset = player.transform.position - transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.position = player.transform.position - offset;
 
         horizontalCamMovement = Input.GetAxis("HorizontalCamMovement");
     }
 
     private void FixedUpdate()
     {
         moveCamera();
     }
 
     void moveCamera()
     {
         if (horizontalCamMovement > 0)
         {
             Debug.Log("You are pressing the E key");
 
             transform.Rotate(Vector3.up, horizontalCamMovement * Time.deltaTime * rotationSpeed);
         }
         
         if (horizontalCamMovement < 0)
         {
             Debug.Log("You are pressing the q key");
             transform.Rotate(Vector3.up, horizontalCamMovement * Time.deltaTime * rotationSpeed);
         }
 
 
     }
 }
 

I have seen some solutions online but can't seem to understand where the code comes from. If anyone could help me out and give me a detailed explanation so that I could learn I would really appreciate it. If you have any questions about my question be sure and let me know and I will answer them :) Thank You

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

0 Replies

· Add your reply
  • Sort: 

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

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

When attaching my custom camera script camera shakes when player starts to move fast. 0 Answers

Help about understanding ceratin command problem 1 Answer

I want my camera to move only Y axis and my character to rotate with my camera 0 Answers

When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer

Does anyone know the script for a smooth camera follow of the main game object? 8 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