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 CJay7 · Jan 09, 2014 at 09:48 PM · javascriptmovementnetworkingmultiplayer

How do I add gravity to my object, and how do I fix my network problem?

Hello,

I am trying to create a small game, to start with, in Unity3D. This is supposed to be a basic game that works over a network. So far, I have my network working, and I had it where one player could see another player moving, and vice versa. Here are my problems: 1) I don't know how to add gravity to my cube (player). Using Rigidbody has not worked, because it's caused my cube to fly all over the screen. 2) I don't know how to fix the problem I am having with my network. At the moment, when one player moves his or her character, it actually moves the other player's character, and vice versa.

You can view this YouTube video to see my problems in action.

Also, when I add gravity to the player/character in another test game (not the one in the video), it tips over based on the slope of the terrain. I want the character (which is a 2x1x1 cube) to stand upright, regardless of the terrain setting.

Here is some code from the game that is shown in the video:

Movement.js Code:

 var speed:float = 0.2;
 var rotateSpeed:float = 5;
 
 function Start(){
     if(!networkView.isMine)
     {    
         enabled = false;
     }
 }
 
 function Update(){
         if(Input.GetKey(KeyCode.UpArrow))
         {
             transform.Translate(Vector3.forward * speed);
         }
         if(Input.GetKey(KeyCode.DownArrow))
         {
             transform.Translate(Vector3.back * speed);
         }
         if(Input.GetKey(KeyCode.RightArrow))
         {
             transform.Rotate(Vector3.up * rotateSpeed, Space.World);
         }
         if(Input.GetKey(KeyCode.LeftArrow))
         {
             transform.Rotate(Vector3.down * rotateSpeed, Space.World);
         }
 }

NetworkManagerScript.js Code:

 var playerPrefab:GameObject;
 var spawnObject:Transform;
 
 var gameName:String = "CJay7_Test";
 
 private var refreshing:boolean;
 private var hostData:HostData[];
 
 private var buttonX:float;
 private var buttonY:float;
 private var buttonWidth:float;
 private var buttonHeight:float;
 
 function Start(){
     buttonX = Screen.width * 0.05;
     buttonY = Screen.width * 0.05;
     buttonWidth = Screen.width * 0.15;
     buttonHeight = Screen.width * 0.15;
 }
 
 function startServer(){
     Network.InitializeServer(32, 25001, !Network.HavePublicAddress);
     MasterServer.RegisterHost(gameName, "Test Game", "I am creating a test game from a tutorial. 'http://vimeo.com/33996023#'");
 }
 
 function refreshHostList() {
     MasterServer.RequestHostList(gameName);
     refreshing = true;
 }
 
 function Update(){
     if (refreshing)
     {
         if (MasterServer.PollHostList().Length > 0)
         {
             refreshing = false;
             Debug.Log(MasterServer.PollHostList().Length);
             hostData = MasterServer.PollHostList();
         }
     }
 }
 
 function spawnCharacter(){
     Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
 }
 
 //MESSAGES
 function OnServerInitialized(){
     Debug.Log("Server Initialized");
     spawnCharacter();
 }
 
 function OnConnectedToServer(){
     spawnCharacter();
 }
 
 function OnMasterServerEvent(mse:MasterServerEvent){
     if (mse == MasterServerEvent.RegistrationSucceeded)
     {
         Debug.Log("Registration Succeeded");
     }
 }
 
 //GUI
 function OnGUI(){
     if(!Network.isClient && !Network.isServer){
         if (GUI.Button(Rect(buttonX, buttonY, buttonWidth, buttonHeight), "Start Server"))
         {
             Debug.Log("Starting Server");
             startServer();
         }
 
         if (GUI.Button(Rect(buttonX, buttonY * 1.2 + buttonHeight, buttonWidth, buttonHeight), "Refresh Hosts"))
         {
             Debug.Log("Refreshing");
             refreshHostList();
         }
         
         if (HostData)
         {
             for (var i:int = 0; i < hostData.length; i++)
             {
                 if(GUI.Button(Rect(buttonX * 2 + buttonWidth, buttonY * 1.2 + (buttonHeight * i), buttonWidth * 3, buttonHeight * .5), hostData[i].gameName))
                 {
                     Network.Connect(hostData[i]);
                 }
             }
         }
     }
 }

Again, please view this YouTube video to see my problems in video form. Thank you in advance for any constructive and helpful comments.

Comment
Add comment · Show 10
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 Invertex · Jan 09, 2014 at 10:51 PM 1
Share

If you don't want it to rotate when hitting the terrain, just set your Rigidbody to Freeze the X,Y,Z rotations. You'll find this under the "Constraints" section of the Rigidbody. You need to use a Rigidbody if you want to use gravity. Your object flying all over the place is likely due to using Transform and Rotate ins$$anonymous$$d of rigidbody.velocity or rigidbody.AddForce to move the character. Or you have a rigidbody on a child object, ins$$anonymous$$d of the top parent.

As for the player issue, this seems to be due to your $$anonymous$$ovement script having no way to identify between the two players... So it's going to detect Player 2's Input and move as well.

avatar image thornekey Invertex · Jan 09, 2014 at 11:14 PM 0
Share

i had a similar problem where it controlled the opposite player - with still no result. this was a while ago. perhaps you could shed some light on my topic too, and this link could be of reference to anyone else having troubles. maybe it will solve someone's problem.

http://answers.unity3d.com/questions/572938/players-not-moving-properly-network-multiplayer.html

avatar image Invertex Invertex · Jan 10, 2014 at 12:46 AM 0
Share

Well, I'm not familiar with working with multiplayer features, that second part was just an educated guess. But I did a quick search and found this thread that seems to have 2 simple answers that would work depending on scenario: http://forum.unity3d.com/threads/25452-$$anonymous$$ultiplayer-Inputs

It looks like C$$anonymous$$7 is using the first method though, so not sure. $$anonymous$$aybe if he used this.enabled ins$$anonymous$$d, it would work. Just a guess though.

avatar image CJay7 Invertex · Jan 10, 2014 at 01:19 AM 0
Share

Hello,

Thank you for the response. I can't seem to find "Fixed Angle" in the Inspector for Rigidbody. Where is this located? I will check out the link for $$anonymous$$ultiplayer Inputs. I tried "this.enabled = false;", ins$$anonymous$$d of "enabled = false;", and it still didn't work. For some reason, I accidentally typed "this.enabledenabled = false;", and this worked for ONE of the screens, but not for both screens. I'm not sure why "networkView.is$$anonymous$$ine" is not see$$anonymous$$g to work on my game...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CJay7 · Jan 13, 2014 at 06:59 AM

I have solved both of my issues. I will try to remember to post my solutions tomorrow.

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 Invertex · Jan 25, 2014 at 11:01 AM 1
Share

Just a re$$anonymous$$der to post what your solution was!

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

setup a php master server 2 Answers

Unity networking tutorial? 6 Answers

(C#) Help with Multiplayer Features - Photon Unity Networking 0 Answers

Online Player Position 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