This error keeps coming up over and over, no matter what I try, and I haven’t been able to wrap my head around the problem. I am trying to change a Transform from another GameObject to a Vector3, but nothing is working. Please help me! The actual error says:
Assets\Scripts\PlayerMovement.cs(36,41): error CS0176: Member ‘Vector3.forward’ cannot be accessed with an instance reference; qualify it with a type name instead
Show us your code (use code tags READ THIS ).
My blind guess is that you’re trying to overwrite the transform with a Vector3.forward. You probably want to overwrite the transform.position, but we only can be sure if you show us your code.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Vector3 PlayerCameraRotation;
[SerializeField] private Transform PlayerCamera;
void Update(){
if(Input.GetMouseButton(0)){
PlayerCameraRotation = new Vector3(PlayerGroundCheck.transform.position.x, PlayerGroundCheck.transform.position.y, PlayerGroundCheck.transform.position.z);
PlayerBody.AddRelativeForce(PlayerCameraRotation.forward * PlayerThrust);
}
}
)
And by the way yes, I have correctly connected the PlayerCamera Transform
This should be Vector3.forward because it is simply a static member returning a new Vector3(0, 0, 1f);
This is also redundant:
PlayerCameraRotation = new Vector3(PlayerGroundCheck.transform.position.x, PlayerGroundCheck.transform.position.y, PlayerGroundCheck.transform.position.z);
It can be simplified to:
PlayerCameraRotation = PlayerGroundCheck.transform.position;
My problem is that I want the current object to have added force going in the forward direction of PlayerCamera. Is there any way I can do that? It would save a lot of headaches. Although thank you for your replies.
Firstly. You get that error because PlayerCameraRotation is a Vector3 and not a Transform. A Vector3 is just a point in space or a direction. It does not have a forward attribute when you use it as an instance of an object. If you use it as a static class member, than you have access to Vector3.forward which is the same as new Vector3(0,01)
For your Problem:
You need the transform of the PlayerCamera which you already have in your code at line 8. So replace the followin
- PlayerBody.AddRelativeForce(PlayerCameraRotation.forward * PlayerThrust);
with this:
PlayerBody.AddRelativeForce(PlayerCamera.forward * PlayerThrust);
Thank you, that is really helpful! It is working now.