- Home /
Question by
unity_m5wEusjvh_p5GQ · Nov 15, 2018 at 02:41 PM ·
movementrigidbody2dvelocityleft
can't move player rigidbody.velocity
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Obstacle : MonoBehaviour { Rigidbody2D rb ; public float moveSpeed ; private void awake () { GetComponent () ; } private void fixedUpdate () { GetComponent ().velocity=( Vector2.left * moveSpeed);
}
void Update () {
if (transform.position.x < -5f)
Destroy(gameObject);
}
}
Comment
Answer by Matt1000 · Nov 15, 2018 at 02:46 PM
You are using GetComponent() the wrong way. This is probably what you want to do:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour {
Rigidbody2D rb;
public float moveSpeed;
private void awake () {
rb = GetComponent<Rigidbody2D> () ;
}
private void fixedUpdate () {
rb.velocity = Vector2.left * moveSpeed;
}
void Update () {
if (transform.position.x < -5f)
Destroy(gameObject);
}
}
always remember to specify which component you want to get and assign it to the variable. Then just use the variable. Hope it helps and if it does mark as accepted ; )