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 Zinatulin · May 01, 2015 at 04:22 PM · c#rigidbody

(Closed)I have a Issue with rigidbody and don't know how to fix it.

Hi there. I'm kind of new to unity and I want to learn scripting using it. But at this point I have a issue and I don't know why it is happening nor how to fix it. Here is the deal. I'm using rigidbody forces ("AddRelativeForce") to move the player. The problem is that when the player is looking to "Z" axis the player moves to "Z" axis, when he is looking to "-X" axis he moves to "-X" axis but when the player looks between the "Z" and "-X" he doesn't moves to that direction, he moves slightly different way.

Here is the problem (sorry for my bad paint skills xD).

alt text

Here is the script that I have in the project:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovementeScript : MonoBehaviour 
 {
     private Rigidbody rb;
 
     private float movementeSpeed;
     private float maxMovementeSpeed;
     private float xRotation;
     private float yRotation;
     private float rotationSpeed;
 
     private GameObject mainCameraObject;
 
     private Vector2 speedController;
 
     void Start () 
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
 
         mainCameraObject = GameObject.Find("Main Camera");
 
         rotationSpeed = 15f;
         maxMovementeSpeed = 2f;
         movementeSpeed = 100f;
 
         rb = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate () 
     {
         MovementeSpeedController();
 
         PlayerCameraController();
     }
 
     private void MovementeSpeedController()
     {
         //speedController is the horizontal movemente of the player so he can controle the speed
         speedController = new Vector2(rb.velocity.x, rb.velocity.y);
 
 
         //Checking if speed is higher than max speed allowed
         if(speedController.magnitude > maxMovementeSpeed)
         {
             speedController = speedController.normalized;
             speedController *= maxMovementeSpeed;
         }
 
 
         //Add force to the player
         rb.AddRelativeForce(Input.GetAxis("Horizontal") * movementeSpeed, 0, Input.GetAxis("Vertical") * movementeSpeed);
 
         //Set the player speed to control it
         rb.velocity = new Vector3(speedController.x, rb.velocity.y, speedController.y);
 
 
         //Setting the run speed or walk speed
         if(Input.GetKey(KeyCode.LeftShift))
         {
             maxMovementeSpeed = 5f;
         }
         else
         {
             maxMovementeSpeed = 2f;
         }
     }
 
     private void PlayerCameraController()
     {
         xRotation += Input.GetAxis("Mouse X") * Time.deltaTime * rotationSpeed;
         yRotation -= Input.GetAxis("Mouse Y") * Time.deltaTime * rotationSpeed;
 
         //Limiting yRotation so the player doesn't flips his neck xD
         yRotation = Mathf.Clamp(yRotation, -90, 90);
 
         mainCameraObject.transform.rotation = Quaternion.Euler(yRotation, xRotation, 0);
 
         rb.rotation = Quaternion.Euler(0, mainCameraObject.transform.rotation.eulerAngles.y, 0);
     }
 }
 

Sorry for my bad English :s and thank you.

playermovemente.png (9.9 kB)
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 Sessional · May 01, 2015 at 04:54 PM 0
Share

Are you trying to make it move forward? If so there is another option that is quite possibly simpler than the RelativeForce.

avatar image Zinatulin · May 01, 2015 at 06:29 PM 0
Share

I forgot to mention, it is a FPS game. And yes, I'm trying to make him move forward.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zinatulin · May 03, 2015 at 12:11 AM

I've found the issue, it is on line 42:

 speedController = new Vector2(rb.velocity.x, rb.velocity.y);

I should have:

 speedController = new Vector2(rb.velocity.x, rb.velocity.z);

But thank you anyway for your time :)

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Stacking physics objects? 1 Answer

Jumping with rigid body3d 1 Answer

how to keep a "ship" over a platform or rigidbody? 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