Author Topic: More complex movement of player  (Read 3522 times)

jkrassman

  • Guest
More complex movement of player
« on: March 06, 2013, 01:12:02 AM »
I can't wrapp this one around my head ;)

So I have been following the "game" tutorial and it seemed straight forward - and I picked up some nice coding in the end. I am in the learning phase of C# and therefore I do get stucked from times to times. I have never been working with networking and either with tasharens assets so I am like a bambi on ice :D


The tutorial game used a touchcontroller and the objects moved across the ground. I would like to know how to implement a little more real world "gameObject" that moves in all directions and using a thruster (Spaceship).

In my controlscript I am using these methods for steering:

  1. _cacheRigidbody.AddRelativeTorque(new Vector3(0,0,-Input.GetAxis("Horizontal")*rollRate*_cacheRigidbody.mass));
  2. _cacheRigidbody.AddRelativeTorque(new Vector3(0,Input.GetAxis("Horizontal")*yawRate*_cacheRigidbody.mass,0));
  3. _cacheRigidbody.AddRelativeTorque(new Vector3(Input.GetAxis("Vertical")*pitchRate*_cacheRigidbody.mass,0,0));
   

and for thrust:

  1. _cacheRigidbody.AddForceAtPosition (transform.forward * thrusterForce, transform.position);

And this is the class from the tutorial:

  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. public class GamePlayer : TNBehaviour {
  6.        
  7.         static public GamePlayer instance;
  8.        
  9.         Vector3 mTarget = Vector3.zero;
  10.        
  11.        
  12.         public Vector3 target{
  13.                
  14.                 set {
  15.                         tno.Send("onSetTarget",Target.AllSaved,value);
  16.                         }
  17.         }
  18.        
  19.  
  20.         void Awake(){
  21.                
  22.                 if(TNManager.isThisMyObject) { 
  23.                        
  24.                                 instance = this;
  25.                         }
  26.                
  27.                 }
  28.        
  29.         void Update(){
  30.                 transform.position      = Vector3.MoveTowards(transform.position,mTarget, 3f * Time.deltaTime);
  31.         }
  32.        
  33.        
  34.         void OnNetworkPlayerJoin(Player p){
  35.                 tno.Send("onSetTargetImediate",p,transform.position);
  36.         }
  37.        
  38.         [RFC]
  39.         void onSetTarget( Vector3 pos){
  40.                
  41.                 mTarget = pos;
  42.         }
  43.        
  44.         [RFC]
  45.         void onSetTargetImediate(Vector3 pos){
  46.                 transform.position = pos;
  47.         }
  48. }
  49.  

Any got a feeling for, how this should be implemented?

Joakim





ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: More complex movement of player
« Reply #1 on: March 06, 2013, 02:53:22 PM »
After applying your torque and force, sync the transform's position, rotation, rigidbody's velocity and angular velocity. As I mentioned in the email -- sync the effect, not the cause.

That said, it's generally easier to have input values set some controller values (if you had a ship controller, imagine it having XYZ torque and thrust values). These would then be the values you'd sync, with a periodic update of position/rotation for good measure. As an added benefit this way these controller values can be changed via input, or via AI -- and your sync logic would remain the same.
« Last Edit: March 06, 2013, 02:55:44 PM by ArenMook »