I want to move my gameobject in the facing direction so i have tried to use AddRelativeForce, but my object does not seems to move at all. I have my speed value to 60, so is not 0. Please help me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float rotationSpeed;
float horizontal;
float vertical;
Rigidbody2D rb2D;
void Start ()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
}
void Update ()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
rb2D.AddRelativeForce(Vector3.forward * speed);
transform.Rotate(0.0f, 0.0f, -horizontal * rotationSpeed);
}
}
Yes, I need to access variables inside the foreach loop to use them outside the loop
– SuperStarGaming235