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 /
avatar image
0
Question by netkingZ · Apr 01, 2013 at 04:23 PM · cameramovementcharacteraxes

character movement follows the direction of camera

Hi i have this script in c# :

 public class PlayerMove : MonoBehaviour {
     public float speed;
     public float moveSpeed = 10;
     public float rotateSpeed = 10;
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         float moveForward = moveSpeed * Time.smoothDeltaTime * Input.GetAxis("Vertical");
         float moveLeft = moveSpeed *  Time.smoothDeltaTime * Input.GetAxis("Horizontal");
         float rotate = rotateSpeed *  Time.smoothDeltaTime * Input.GetAxis("Horizontal");
         
         transform.Translate(Vector3.forward * moveForward);
         transform.Translate(Vector3.right * moveLeft);
             
             
     
     }
 }

i want that the character movement on base the direction of camera. So the axes of camera decides the direction of the character , if i rotated the camera to right and i click "up arrow" ( forward movement) the my character go to right but if i don't have rotated the camera but i click to "right arrow" my character moved laterally to right. I hope I explained. For instance, do you remember the games on Conole? here is the same type of dynamic motion that i want reach with the chamber and the movement of the same character, for example of a game such as Call of Duty this is .

Please help me. Thank you

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 netkingZ · Apr 02, 2013 at 03:49 AM 1
Share

Help me. Thank you

avatar image ByteSheep · Apr 02, 2013 at 03:56 AM 0
Share

Hah there are -1 answers to this question.
But, to give you a tip, the lines you will want to change are lines 18 and 19.
Specifically you will want to change "Vector3.forward" to the cameras forward direction.
Vector3.forward should get the forward axis in world space, transform.forward will get the forward axis of the object the script is attached to.
SO, what you want to do is add a reference to the camera object and get it's forward axis ins$$anonymous$$d e.g. cam.transform.forward and cam.transform.right

avatar image netkingZ · Apr 02, 2013 at 08:04 AM 0
Share

Thank You , this is the new script c# with the modify (is present the Jump) :

 public class Player$$anonymous$$ove : $$anonymous$$onoBehaviour {
   
 
 // These variables are for adjusting in the inspector how the object behaves 
 public static float maxSpeed  = 7;
 public static float force     = 8;
 public static float jumpSpeed = 0;
 public GameObject cam;
  
 // These variables are there for use by the script and don't need to be edited
 private int state = 0;
 private bool grounded = false;
 private float jumpLimit = 0;
  
 // Don't let the Physics Engine rotate this physics object so it doesn't fall over when running
 void Awake ()
 { 
     rigidbody.freezeRotation = true;
 }
  
 // This part detects whether or not the object is grounded and stores it in a variable
 void OnCollisionEnter ()
 {
     state ++;
     if(state > 0)
     {
         grounded = true;
     }
 }
  
  
 void OnCollisionExit ()
 {
     state --;
     if(state < 1)
     {
         grounded = false;
         state = 0;
     }
 }
  
  
 public virtual bool jump
 {
     get 
     {
         return Input.GetButtonDown ("Jump");
     }
 }
  
 public virtual float horizontal
 {
     get
     {
         return Input.GetAxis("Horizontal") * force;
     } 
 } 
 public virtual float vertical
 {
     get
     {
         return Input.GetAxis("Vertical") * force;
     } 
 }
 // This is called every physics frame
 void FixedUpdate ()
 {
  
  
  
     // If the object is grounded and isn't moving at the max speed or higher apply force to move it
     if(rigidbody.velocity.magnitude < maxSpeed && grounded == true)
     {
         rigidbody.AddForce (transform.rotation * cam.transform.forward * vertical);
         rigidbody.AddForce (transform.rotation * cam.transform.right * horizontal);
     }
  
     // This part is for jumping. I only let jump force be applied every 10 physics frames so
     // the player can't somehow get a huge velocity due to multiple jumps in a very short time
     if(jumpLimit < 10) jumpLimit ++;
  
     if(jump && grounded  && jumpLimit >= 10)
     {
         rigidbody.velocity = rigidbody.velocity + (Vector3.up * jumpSpeed);
         jumpLimit = 0;
     }
  }

 

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

11 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

Related Questions

Camera relative movement 0 Answers

character turning where he looks 0 Answers

Newbie Question 2 Answers

Smooth Camera Movement script with problem. 0 Answers

Make a camera move towards player when the player moves out of the camera's vision 2 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