Search Unity

how can i beter make an movescript??

Discussion in 'Scripting' started by olivierus, Aug 28, 2012.

?

What is the best way to make move script

  1. Transform

    33.3%
  2. Ridgidbody

    100.0%
Multiple votes are allowed.
  1. olivierus

    olivierus

    Joined:
    Nov 26, 2010
    Posts:
    89
    he everybody,

    i just got a question about wich way is the best to make a good move script.

    at the moment i got this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMove : MonoBehaviour {
    5.  
    6.     public int Speed;
    7.     public float Jumpheigt;
    8.     public bool Jump = false;
    9.    
    10.     private int totalScore;
    11.  
    12.     void FixedUpdate ()
    13.     {
    14.         if(Input.GetKey(KeyCode.D))
    15.         {
    16.             rigidbody.AddForce(Vector3.back * Speed);
    17.         }
    18.         if(Input.GetKey(KeyCode.A))
    19.         {
    20.             rigidbody.AddForce(Vector3.forward * Speed);
    21.         }
    22.         if(Input.GetKey(KeyCode.W))
    23.         {
    24.             rigidbody.AddForce(Vector3.up * Jumpheigt);
    25.         }
    26.     }
    27.    
    28.    
    29. }
    30.  
    and i work with the ridgidbody, but is not even close to what i wanted.

    what would you guys(or girls ) propose me to work with??

    TRANSFORM or RIDGIDBODY??

    Thanx a lot

    olivierus
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Well what do you want?

    Have you tried Character Controller?

    There's not a 'best'; transform is required for some things and rigidbody for others.
     
  3. olivierus

    olivierus

    Joined:
    Nov 26, 2010
    Posts:
    89
    oke, but i am making some sort of side scroller. so the character controller wasn't the best way. but won't know wich method should be te best for : High jumps, smooth walking enz.
     
  4. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    273
    Here is a 2d character controller i just did. i hope it helps. I'm no programmer so if anyone see anything that doesn't look correct please correct it for me. thanks.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var speed : float = 1.0;
    5. var jumpHeight : float = 2.0;
    6. var gravity : float = 5.0;
    7.  
    8. private var jump : boolean = false;
    9. private var moveDirection : Vector3 = Vector3.zero;
    10. private var characterController : CharacterController;
    11.  
    12. function Awake () {
    13.     characterController = GetComponent(CharacterController);
    14. }
    15.  
    16. function FixedUpdate () {
    17.     // Make sure we are absolutely always in the 2D plane.
    18.     transform.position.z = 0;
    19. }
    20.  
    21. function Update()
    22. {
    23.     if(Input.GetKey(KeyCode.D))
    24.     {
    25.         //character will move to the right
    26.         characterController.Move(transform.right * speed * Time.deltaTime);
    27.     }
    28.  
    29.     if(Input.GetKey(KeyCode.A))
    30.     {
    31.         //character will move to the left
    32.         characterController.Move(transform.right * -speed * Time.deltaTime);
    33.     }
    34.  
    35.     if(Input.GetKey(KeyCode.W))
    36.     {
    37.         //character will jump up
    38.         characterController.Move(transform.up * jumpHeight * Time.deltaTime);
    39.         moveDirection.y = jumpHeight;
    40.     }
    41.  
    42.     //always apply gravity to gameobject
    43.     moveDirection.y -= gravity * Time.deltaTime;
    44.     characterController.Move(moveDirection * Time.deltaTime);
    45. }
    46.  
    47. @script RequireComponent (CharacterController)
     
    Last edited: Aug 30, 2012