First Person and Lifting
Hey all,
I'm starting a project where you play in first person. My first question is how to implement that- I tried using the First Person that came with Unity, but I'm unable to look right at left. Secondly, I want to be able to lift other objects so that they hover in front of me and I can walk around with them- Portal style. I'm new to unity and C# and I would really appreciate the help!
Thanks!
Answer by UnityCoach · Feb 24, 2017 at 12:34 AM
Unity suggest a Component based architecture. So, if you're going to use the default First Person Controller, I would suggest to develop a GrabObject component that you put on the same game object and that communicate with it.
You also need a Pickable component that you add to game objects that can be picked up. The pickable object could also simply use an object tag "pickable" and you can put all mechanic in the GrabObject component.
The component will need to :
Find objects in the field of view, or center of the screen. You can use Physics.Raycast and the object tags for this.
Use some Input to trigger the pick-up
Then you can reposition the object and parent it to your 1st Person Controller using its Transform
You can then use some other Input to drop the object, and then use RayCast again to put it back in the world where you stand. The object will need a Collider and RigidBody if you want it to fall down.
This should be enough to get you started. Good luck!
Just a quick question- when I add the Rigidbody First Person Controller I'm able to move, but I can only look up and down- not left and right. I've checked the Input, and everything should be in order. Any ideas? Thanks so much!
I haven't worked with it for a while. Looking at the FirstPersonCharacterGuidelines doc, I see :
The first-person character is made up of a few components acting together. The FirstPersonCharacter script provides the functionality of moving, strafing and jumping. The Simple$$anonymous$$ouseRotator provides the functionality of turning the body of the character left and right, and another copy of the same script on the "FirstPersonCamera" controls the looking-up-and-down effect.
Though, when I look at the Prefab it looks fairly different form this. It happens that they change the API and don't update the assets. Although, it works fine on my version, using 5.5.0p1 on $$anonymous$$ac.
I had to use the prefab that came with the assets, but I was able to modify it to work how I wanted it to. Thanks so much!
Hey UnityCoach!
Sorry to bother you again- one last question.
I'm trying to get the Vector3 transform of that hit object. Right now my code to get that is: hit.collider.transform.gameObject But it gives me the error `UnityEngine.GameObject.transform' cannot be assigned to (it is read-only) '
How would I do this? Thanks so much again!
No problem. So, let me give you a bit of theory :
The transform property links to the Transform component itself, and only offers a get;
The Transform component is a bit special in Unity, it's the only component that cannot exist without a GameObject, as much as a GameObject cannot exist without a Transform.
Anyway, you can't assign pre-built components to GameObjects, as the AddComponent method only takes a type to add a new component, so you usually add the component and modify it right after, as opposed to building it and setting it up before you add it.
Now, with the hit, I understand you're using a RaycastHit. $$anonymous$$now that it has a point property, which returns the point of impact as a Vector3. If you want the position of the hit object, you want to use RaycastHit.transform`.position` which you can read and write. Though, remember that Vector3 is a struct, not a class. So you can't modify one of its components, you have to build a new one and replace it.
hit.transform.position.y += 1f; // won't work, you need to use the following :
Vector3 pos = hit.transform.position;
pos.y += 1f;
hit.transform.position = pos;
Hope this helps.
Your answer
Follow this Question
Related Questions
What is the standard direction of a character relative to its own axis? 1 Answer
How can I teleport the player when a bonus level timer runs out? 0 Answers
Build navmesh during runtime 0 Answers
Partial synchronization of camera with the bone of the head or other object 0 Answers
Virtual hands in FPS MMO game 1 Answer