- Home /
Using a variable as a components for Vector3
How/ is it possible to have variables as the components in a Vector3? I'm trying to make an obstacle that moves around and this is the code that I have. It gives me an error saying I can't convert float to Vector3.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Object_Movement : MonoBehaviour { public Rigidbody rb; public Transform Obstacle; public float sideForce; public float upForce; public float forwardForce; public Vector3 newPosition; public float speed;
// Start is called before the first frame update
void Start()
{
newPosition = (sideForce, upForce, forwardForce);
}
// Update is called once per frame
void FixedUpdate()
{
rb.MovePosition(Obstacle.position + (newPosition * speed * Time.deltaTime));
}
}
Answer by hendryshaikh2004 · Oct 15, 2020 at 07:27 PM
Hey there you can fix it by the following, Unity does not allow you to change vector3 directly so you can do it like this and it works. If it helps please give alike means a lot!
newPosition.x = sideForce;
newPosition.y = UpForce;
newPosition.z = ForwardForce;
Or this:
newPosition = new Vector3(sideForce, upForce, forwardForce);