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 Ninjaoboy · Jun 12, 2011 at 04:27 PM · rigidbodytransformfpscontrollerdiagonal

How to move rigidbody Diagonal with movePosition

 var player :Transform;
 var speed = 10;
 private var direction = Vector3(0,0,0);
 
 private var grounded = 0;
 
 function Update () {
 walking = speed  * Time.deltaTime;
 
 
 
 if(Input.GetKey("w"))
 player.rigidbody.MovePosition(player.position + player.forward * walking);
 
 if(Input.GetKey("d"))
 player.rigidbody.MovePosition(player.position + player.right * walking);
 
 if(Input.GetKey("a"))
 player.rigidbody.MovePosition(player.position + player.right * -walking);
 
 if(Input.GetKey("s"))
 player.rigidbody.MovePosition(player.position + player.forward * -walking);
 
 if(Input.GetKey("w") && (Input.GetKey("d")))
 player.rigidbody.MovePosition(player.position + player.forward.right * Time.deltaTime);
 
 
 
 
 }

there's my whole script so far, and i have seen other for controlling rigidbodys, but i dont what to have a script i cant understand so im making my own, i don't want to use add Force, but if i dont then there is no diagonal its either straight or sideways. how do i make a dagonal?

Comment
Add comment · Show 2
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 Eric5h5 · Jun 12, 2011 at 04:40 PM 0
Share

It's a lot easier if you use GetAxis ins$$anonymous$$d of a bunch of keys.

avatar image KungKras · Jan 06, 2016 at 05:10 PM 0
Share

For some reason my reply didn't show up, despite the message saying that it was waiting for approval dissapearing.

Anyway, it was basically this. You can declare a temporary Vector3 called movement. WASD adds +1 in the direction the buttons correspond to to this vector.

then normalize the vector and multiply it with your speed. And then use moveposition. $$anonymous$$uch simpler than adding && comparisons for every possible button combination.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KungKras · Jan 07, 2016 at 10:26 PM

 Vector3 movement;
 
 if (Input.GetKey (KeyCode.W)) {
     movement = movement + transform.forward;
 }
 if (Input.GetKey (KeyCode.S)) {
     movement = movement - transform.forward;
 }
 if (Input.GetKey (KeyCode.A)) {
     movement = movement + transform.TransformDirection (Vector3.left);
 }
 if (Input.GetKey (KeyCode.D)) {
     movement = movement + transform.TransformDirection (Vector3.right);
 } 
 movement = Vector3.Normalize (movement);
 movement = movement * speed;
 rigidbody.MovePosition (transform.position + movement*Time.fixedDeltaTime);
  



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 DaveA · Jun 12, 2011 at 04:42 PM

Accumulate a 'delta' in a Vector3, then apply it with MovePosition at the end

Comment
Add comment · Show 2 · 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 Ninjaoboy · Jun 12, 2011 at 06:44 PM 0
Share

Could you help me understand that better, i am kind of a noob in scripting, also if it is a Vector3, wouldn't it be the world-wide x,y,z coordinates, they need to be local.

avatar image EpicPandaGamer25 Ninjaoboy · Oct 09, 2015 at 07:59 PM 0
Share

Yes, please help me understand what you are saying!

avatar image
0

Answer by Siloaman · Jan 06, 2016 at 03:45 PM

@Ninjaoboy

using UnityEngine; using System.Collections;

public class rigidbody_controller_script : MonoBehaviour {

 Rigidbody rb;
 float moveSpeed;
 
 public Vector3 straightForward;
 
 public Quaternion spreadAngleRight;
 public Quaternion spreadAngleLeft;
 
 public Vector3 localRightVector;
 public Vector3 localLeftVector;
 
 public Vector3 newVectorRight;
 public Vector3 newVectorLeft;

 void Start () {
 
     moveSpeed = 20.0f;
     
 
 
     straightForward = transform.TransformDirection(Vector3.forward);
     
     spreadAngleRight = Quaternion.AngleAxis(45, new Vector3(0,1,0));
     spreadAngleLeft = Quaternion.AngleAxis(315, new Vector3(0,1,0));
     
     localRightVector = spreadAngleRight * straightForward;
     localLeftVector = spreadAngleLeft * straightForward;
     
     newVectorRight = transform.TransformDirection(localRightVector);
     newVectorLeft = transform.TransformDirection(localLeftVector);

     rb = GetComponent<Rigidbody>();
 
 }
 

 void Update () {
 
 
 
 // REGULAR WASD COMMANDS
 
     if(Input.GetKey(KeyCode.W))
         rb.MovePosition(transform.position + transform.TransformDirection(Vector3.forward * moveSpeed) * Time.deltaTime);
                                                                                             
     if(Input.GetKey(KeyCode.A))
         rb.MovePosition(transform.position + transform.TransformDirection(-Vector3.right * (moveSpeed/2) ) * Time.deltaTime);
         
     if(Input.GetKey(KeyCode.S))
         rb.MovePosition(transform.position + transform.TransformDirection(-Vector3.forward * (moveSpeed/4) ) * Time.deltaTime);    
         
     if(Input.GetKey(KeyCode.D))
         rb.MovePosition(transform.position + transform.TransformDirection(Vector3.right * (moveSpeed/2) ) * Time.deltaTime);
     
 // STRAFING COMMDANDS
     
     if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
         rb.MovePosition(transform.position + transform.TransformDirection(newVectorRight * moveSpeed) * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
         rb.MovePosition(transform.position + transform.TransformDirection(newVectorLeft * moveSpeed) * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
         rb.MovePosition(transform.position - transform.TransformDirection(newVectorLeft * (moveSpeed/4) ) * Time.deltaTime);
         
     if(Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
         rb.MovePosition(transform.position - transform.TransformDirection(newVectorRight * (moveSpeed/4) ) * Time.deltaTime);
 
 
 }

}

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 vanshTandon · Sep 02, 2021 at 01:04 PM 0
Share

How to make this with joystick controls. Please Help

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

8 People are following this question.

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

Related Questions

Player Falls Over 1 Answer

Pick Up Object using Rigidbody FPS 0 Answers

Need Help in FPS controller based on Rigidbody 0 Answers

Why won't my rigidbody fps controller move? 1 Answer

RigidBody FPS Walker Collision Glitch 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