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 TheDevilsHitman · Sep 27, 2016 at 10:38 AM · c#camerascripting problemmovement scriptplatformer

[ C#] Character Movement based on Camera Direction.

I'm new to Unity as of coming from Game Maker to learn something new and try to make a 3D game for the first time. And I wanted to try to make a 3 dimensional platformer that jumps and moves around in 8 directions. At first it was difficult (figuring out damn rotations and so on. But still a noob so forgive me lol). But I cant figure out how to make the players movement based on the cameras direction. I have searched through the forums and have found out most people list the Camera into a variable which is called within the script and they add it into their movement script. I thought it would be easy enough but no matter what I do, Unity just likes to cock block me with a bunch of error messages saying that I cant do what I wrote in the script. The function lists on Unitys website doesnt do a whole to explain what they do in the context of a script and there videos are so few. (Most tutorials online are just people copying and pasting assets when all I want to do is just make codes from scratch and actually learn.) So now I am stuck and resulting to getting help online.

All I want is the movement in all 8 directions like a typical Playstation/Xbox game, nice and simple that works with the Cameras facing direction. Ive heard about how local axis and global axis are a thing but from what I looked up no one has broken it down into pure English on how to do anything with it and just said "just do it".

Anyways, Here is my player script. I will post up the camera script if that is necessary as well. But from what Ive looked up its mainly the movement script that is required to edit. Tell me if I'm wrong because again, I'm new to Unity so any new information would be wonderful.

Player Movement:

 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(Animator))] 
 
 public class PlayerMovement : MonoBehaviour {
 
 
         //Variables.
             Animator Anim;
             Rigidbody rBody;
             Camera cameraobject; 
     CharacterController controller;
 
     //For Movement.
     public int Walkspeed = 0;
     public int Runspeed = 10; 
 
     public Vector3 Vertical;
     public Vector3 Horizontal;
     public Vector3 Stop;
 
     //For Jumping
     public float Gravity = 50.0f; // Gravity
     private float JumpSpd = 2f; // Vertical Velocity
     public float JumpForce = 10.0f; // Force
 
             void Awake () {
         //Required Shit.
         Anim = GetComponent<Animator> ();
         rBody = GetComponent<Rigidbody> ();
         cameraobject = Camera.main;
         controller = GetComponent<CharacterController> ();
 
     }
                 
 
     void Start () {
         //Movement Variables
         Vertical = cameraobject.transform.InverseTransformDirection (Vector3.forward);
         Horizontal = transform.TransformDirection (Vector3.right);    
         Stop = transform.TransformDirection(Vector3.zero);
 
     }
 
     // Update is called once per frame
     void Update () {
         Move ();  
         Jump ();
     }
 
     // Updated constantly even if frame rate skips.
     void FixedUpdate() {
         
     }
         
         //3D Movement.
     void Move () {
 
         //Traditional Up, Down, Left, Right Movement.
         //If on the Ground.
             // Animation.
             Anim.SetFloat ("Vertical_Movement", Input.GetAxis ("Vertical"));
             Anim.SetFloat ("Horizontal_Movement", Input.GetAxis ("Horizontal"));
         /*
         if Stop; {
             Anim.SetFloat ("Stop", 1);
         }
         */
             //Forward
             if (Input.GetAxis ("Vertical") > 0) {
             controller.Move (Vertical * Time.deltaTime * Walkspeed); 
                 transform.localEulerAngles = new Vector3 (0, 180, 0);
                 //Backward
             } else if (Input.GetAxis ("Vertical") < 0) {
                 controller.Move (-Vertical * Time.deltaTime * Walkspeed); //Actual Movement
                 transform.localEulerAngles = new Vector3 (0, 0, 0); // Snap Rotations
             }
 
             //Left
             if (Input.GetAxis ("Horizontal") > 0) {
                 controller.Move (Horizontal * Time.deltaTime * Walkspeed);
                 transform.localEulerAngles = new Vector3 (0, 270, 0);
                 //Right
             } else if (Input.GetAxis ("Horizontal") < 0) {
                 controller.Move (-Horizontal * Time.deltaTime * Walkspeed);
                 transform.localEulerAngles = new Vector3 (0, 90, 0);
             }
 
 
             //Angled movement
             // Up Right.
             if (Input.GetAxis ("Vertical") > 0 && Input.GetAxis ("Horizontal") > 0) {
                 controller.Move (Horizontal * Time.deltaTime / Walkspeed);
                 controller.Move (Vertical * Time.deltaTime / Walkspeed);
                 transform.localEulerAngles = new Vector3 (0, 310, 0);
                 //Down Left
             } else if (Input.GetAxis ("Vertical") < 0 && Input.GetAxis ("Horizontal") < 0) {
                 controller.Move (-Horizontal * Time.deltaTime / Walkspeed);
                 controller.Move (-Vertical * Time.deltaTime / Walkspeed);
                 transform.localEulerAngles = new Vector3 (0, 45, 0); 
             }
             //Up Left
             if (Input.GetAxis ("Vertical") > 0 && Input.GetAxis ("Horizontal") < 0) {
                 controller.Move (-Horizontal * Time.deltaTime / Walkspeed);
                 controller.Move (Vertical * Time.deltaTime / Walkspeed);
                 transform.localEulerAngles = new Vector3 (0, 310, 0);
 
                 //Down Right.
             } else if (Input.GetAxis ("Vertical") < 0 && Input.GetAxis ("Horizontal") > 0) {
                 controller.Move (Horizontal * Time.deltaTime / Walkspeed);
                 controller.Move (-Vertical * Time.deltaTime / Walkspeed);
                 transform.localEulerAngles = new Vector3 (0, 45, 0); 
             }
         
         }
 
 
     //To Jump
     void Jump() {
         //Check if Player is on the Ground.
         if (controller.isGrounded) { 
             JumpSpd = -Gravity * Time.deltaTime; 
             //Then Jump if he is on the Ground.
             if (Input.GetButtonDown ("Jump")) { 
                 JumpSpd = JumpForce; 
             }
         } //Else, Fall.
         else { JumpSpd -= Gravity * Time.deltaTime; }
     //Jump.
         Vector3 moveVector = new Vector3 (0, JumpSpd, 0); 
         controller.Move (moveVector * Time.deltaTime);
 
 
                 }
 
 
 
 
 }
 
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

Answer by hensoup · Dec 23, 2017 at 02:55 AM

https://answers.unity.com/questions/804400/movement-based-on-camera-direction.html

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 IronKnigh · Aug 20, 2018 at 01:34 AM

Although this question was asked approximately 2-3 years ago, I recommend you check out my post where the first part of my solution applies to your question:

https://answers.unity.com/questions/1506873/how-to-animate-player-rotation-with-respect-to-cam.html?childToView=1544087#answer-1544087

If the first solution above doesn't apply to you, I recommend you watch this YouTube video.

https://www.youtube.com/watch?v=ORD7gsuLivE

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 IronKnigh · Aug 20, 2018 at 01:38 AM 0
Share

Ok, so another solution if the one above doesn't apply to you:

https://www.youtube.com/watch?v=_DmUL9$$anonymous$$$$anonymous$$9Yk

Hope this helps!

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

6 People are following this question.

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

Related Questions

Performing sequential actions like moving and dealing damage 1 Answer

I copied all the code in my script for the Roll a Ball tutorial to make the camera follow the ball, but the camera is still not following the ball? (It says no code errors) 2 Answers

Lock player movement within screen bounds 2 Answers

Player not Moving in Test,Debugging not Working Correctly 0 Answers

Rotatearaound a moving object 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