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 AADill · May 17, 2015 at 08:16 PM · mechanics

Moving a character depending on the direction he is facing (C#/Unity3D)

Right now I have a small soldier on the screen that moves with W, A, S, D and rotates / changes direction with the arrow keys. What I want to happen is when the player's direction changes (by using the arrow keys), pushing W will send him in that direction instead of up the screen.

Also (for example), if you are holding D and the right arrow key at the same time he should move in a circle about a point on the ground (instead of spinning in a circle while moving in a straight line to the right).

Here is my current code:

 public class MovePlayer : MonoBehaviour {
   void Update() {
     // Rotate left
     if (Input.GetKey (KeyCode.LeftArrow)) {
       transform.Rotate(0, 0, 1.3f);
     }
     // Rotate right
     if (Input.GetKey(KeyCode.RightArrow)) {
       transform.Rotate(0, 0, -1.3f);
     }
     // Strafe left
     if (Input.GetKey (KeyCode.A)) { 
      Vector3 position = this.transform.position;
       position.x -= 0.055f;
       this.transform.position = position;
     }
     // Move up        
     if (Input.GetKey (KeyCode.W)) {
       Vector3 position = this.transform.position;
         position.y += 0.043f;
         this.transform.position = position;
     } 
     // Move down        
     if (Input.GetKey (KeyCode.S)) {
       Vector3 position = this.transform.position;
       position.y -= 0.043f;
       this.transform.position = position;
       isMoving = true;
     }
     // Strafe right        
     if (Input.GetKey (KeyCode.D)) {
       Vector3 position = this.transform.position;
       position.x += 0.055f;
       this.transform.position = position;
     } 
 }

}

Any ideas? Wasn't sure what to search exactly.

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 Onk3y · Aug 05, 2017 at 06:29 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class movePlayer : MonoBehaviour {
     public float moveSpeed = 15f;
     public float turnSpeed = 200f;
     
     void Update () {
         transform.position += transform.forward * Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
         transform.position+= transform.right* Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
         transform.Rotate(Vector3.up * Input.GetAxis("Another Horizontal") * turnSpeed * Time.deltaTime);
     }
 }

This is probably the easiest way of doing this. Hit me up if you have any questions ;)

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Need help with launching mechanic 0 Answers

Im trying to create a word game with a twist 0 Answers

Leaning/peeking 1 Answer

How does Unity3d uses nvidia 0 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