How do I use the GetComponent to access the RigidBody?
I don't really know about C#sharp. Could I get help? This is my program. I'm trying to get the user input to control the player. I cant get the RigidBody component.
using UnityEngine; using System.Collections;
[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }
public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Rigidbody.velocity = movement;
}
}
Answer by Taxen0 · Nov 02, 2015 at 03:05 PM
Have a look at: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
First you should create a variable of type Rigidbody and in the Awake you set that variable to the actual component, so that you can use that variable to access the rigidbody later.
Example: (not tested!)
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
private Rigidbody rb;
void Awake(){
rb = gameObject.GetComponent<Rigidbody>();
}
private void Example() {
rb.velocity = some value;
}
}
Also have a look at http://docs.unity3d.com/ScriptReference/Rigidbody.html if you haven't already. there are other ways to move the object instead of changing the velocity directly.
Hope you found this helpful!
Thanks for the help! $$anonymous$$y program is working much better now. All I need to do is figure out what's wrong with the classes. Other than that, it is perfect!
Answer by himanshugupta159 · Jun 15, 2018 at 07:34 AM
Check Out the below linq it provide's the information of using get component:link text
Your answer
Follow this Question
Related Questions
Rigidbody velocity and AddForce overrides,Rigidbody.velocity overrides the Force 0 Answers
My player doesn't rotate at the same time with the camera. 0 Answers
how to change the velocity of a rigid body based on a forward 0 Answers
I can't move my player in a fps? 1 Answer
3D Pong Clone: Set position of a Rigidbody, or change how a speed vector is verified? 1 Answer