Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

i need to change Rigidbody2d.velocity for Character Controller

Discussion in 'Scripting' started by darkymdq, Feb 26, 2015.

  1. darkymdq

    darkymdq

    Joined:
    Jun 4, 2014
    Posts:
    9
    this is my code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.    
    6.     public float Speed = 2;
    7.     Animator anim;
    8.    
    9.     void Start (){
    10.         anim = GetComponent<Animator> ();
    11.     }
    12.    
    13.     void FixedUpdate() {
    14.         rigidbody2D.velocity = new Vector2 (Input.GetAxis ("Horizontal") * Speed, 0);
    15.         anim.SetFloat ("hSpeed", Mathf.Abs (rigidbody2D.velocity.x));
    16.        
    17.     }
    18. }
    i use a character Controller for the First person Controller, , but if i change " rigidbody2D.velocity " i get error

    How can i fix this ???
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  3. darkymdq

    darkymdq

    Joined:
    Jun 4, 2014
    Posts:
    9
    yes , i allready read that

    but i don't know as the code is added
     
  4. darkymdq

    darkymdq

    Joined:
    Jun 4, 2014
    Posts:
    9
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement2 : MonoBehaviour {
    5.     Animator anim;
    6.  
    7.     void Start (){
    8.         anim = GetComponent<Animator> ();
    9.     }
    10.  
    11.  
    12.     void Update() {
    13.         CharacterController controller = GetComponent<CharacterController>();
    14.         Vector3 horizontalVelocity = controller.velocity;
    15.         horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
    16.         float horizontalSpeed = horizontalVelocity.magnitude;
    17.         float verticalSpeed = controller.velocity.y;
    18.         float overallSpeed = controller.velocity.magnitude;
    19.         anim.SetFloat ("hSpeed", Mathf.Abs (CharacterController.velocity.x));
    20.  
    21.     }
    22. }
    not working
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  6. darkymdq

    darkymdq

    Joined:
    Jun 4, 2014
    Posts:
    9
    i need that line, so i can get the animation work
     
  7. darkymdq

    darkymdq

    Joined:
    Jun 4, 2014
    Posts:
    9
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement2 : MonoBehaviour {
    5.  
    6.     public float Speed = 2;
    7.     Animator anim;
    8.  
    9.     void Start (){
    10.         anim = GetComponent<Animator> ();
    11.     }
    12.  
    13.  
    14.     void Update() {
    15.         CharacterController controller = GetComponent<CharacterController> ();
    16.         Vector2 horizontalVelocity = controller.velocity;
    17.         horizontalVelocity = new Vector2 (Input.GetAxis ("Horizontal") * Speed, 0);
    18.         float horizontalSpeed = horizontalVelocity.magnitude;
    19.         float verticalSpeed = controller.velocity.y;
    20.         float overallSpeed = controller.velocity.magnitude;
    21.     }
    22.         void FixedUpdate() {
    23.         CharacterController.velocity = new Vector2 (Input.GetAxis ("Horizontal") * Speed, 0);
    24.         float anim ("hSpeed", Mathf.Abs(CharacterController.velocity.x));
    25.  
    26.     }
    27. }
    this work, but i have error in one line :

    Assets/Scripts/Movement 2.cs(24,28): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
     
  8. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I am too friendly....

    You are not taking your instance of CharacterController here, instead you reference the class itself. You need to reference you instance "controller":
    Code (CSharp):
    1. anim.SetFloat ("hSpeed", Mathf.Abs (controller.velocity.x));
    Also make sure you place all physics code in FixedUpdate.

    And create a reference to you Character Controller gameObject in either Start or even better Awake.
     
    darkymdq likes this.
  9. darkymdq

    darkymdq

    Joined:
    Jun 4, 2014
    Posts:
    9
    ok that work exelent !!, ty !