is this C# or js ?
I found the code on internet i want to use as a base and for learning as i want to make something similar to it.
The question is if i put code into js or C# folder i get errors on both so i dont know which one is it.
public class HeldObject : MonoBehaviour {
public bool IsColliding { get; private set; }
private void OnCollisionEnter(Collision collision)
{
IsColliding = true;
}
private void OnTriggerStay(Collider other)
{
IsColliding = true;
}
private void OnCollisionExit(Collision collision)
{
IsColliding = false;
}
}
public class PlayerPick : MonoBehaviour
{
// Maximum distance from the camera at which the object can be picked up
public float MaxPickDistance;
public float ThrowStrength = 10;
public float HeldObjectFollowStrength = 50;
private Player _player;
private Camera _playerCam;
private GameObject _pivot;
private HeldObject _heldObject; // physical avatar
private GameObject _heldBodyAvatar; // visual avatar
private float _heldBodyAngularDrag;
private RaycastHit? _raycast;
private void Start()
{
_player = Player.Instance;
_playerCam = Player.Camera;
_pivot = Player.PickPivot;
}
private void Update()
{
Raycast();
if (Input.GetKeyDown(KeyCode.E))
{
if (!_heldObject)
TakeObject();
else
ReleaseObject();
}
HoldObject();
if (Input.GetMouseButton(0) && _heldObject)
ThrowObject();
}
private void Raycast()
{
_raycast = null;
const int layerMask = 1 << 8;
var raycastHits = Physics.RaycastAll(_playerCam.transform.position, _playerCam.transform.forward,
MaxPickDistance, ~layerMask);
foreach (var hit in raycastHits)
{
if (hit.collider == _player.collider || !hit.collider.rigidbody) // avoid colliding with the player object itself
continue;
_raycast = hit;
}
}
private void HoldObject()
{
if (!_heldObject)
return;
// Drag the physical avatar by changing its velocity
_heldObject.rigidbody.velocity = HeldObjectFollowStrength * (_pivot.transform.position - _heldObject.transform.position);
// When the physical avatar collides, we move the visual one to the same position. When this doesn't happen,
// move the visuals back to the pivot point.
_heldBodyAvatar.transform.position = _heldObject.IsColliding
? _heldObject.transform.position
: _pivot.transform.position;
// If the physical avatar is colliding with something, change visuals rotation to correspond
if (_heldObject.IsColliding)
_heldBodyAvatar.transform.rotation = _heldObject.transform.rotation;
}
private void TakeObject()
{
if (!_raycast.HasValue)
return;
var heldBody = _raycast.Value.rigidbody;
heldBody.transform.position = _pivot.transform.position;
heldBody.useGravity = false;
_heldBodyAngularDrag = heldBody.angularDrag;
heldBody.angularDrag = 1; // so that the object doesn't continue rotating after each collision
heldBody.renderer.enabled = false;
_heldObject = heldBody.gameObject.AddComponent<HeldObject>();
_heldBodyAvatar = new GameObject("Held body avatar");
_heldBodyAvatar.transform.parent = _playerCam.transform;
_heldBodyAvatar.transform.position = _pivot.transform.position;
_heldBodyAvatar.transform.rotation = heldBody.transform.rotation;
_heldBodyAvatar.transform.localScale = heldBody.transform.localScale;
_heldBodyAvatar.AddComponent<MeshFilter>().sharedMesh = heldBody.gameObject.GetComponent<MeshFilter>().sharedMesh;
_heldBodyAvatar.AddComponent<MeshRenderer>().sharedMaterial = heldBody.gameObject.renderer.sharedMaterial;
Physics.IgnoreCollision(heldBody.collider, Player.Controller, true);
}
private void ReleaseObject(Action onRelease = null)
{
Physics.IgnoreCollision(_heldObject.collider, Player.Controller, false);
_heldObject.rigidbody.useGravity = true;
_heldObject.rigidbody.angularDrag = _heldBodyAngularDrag;
_heldObject.rigidbody.transform.position = _heldBodyAvatar.transform.position;
_heldObject.rigidbody.transform.rotation = _heldBodyAvatar.transform.rotation;
_heldObject.rigidbody.renderer.enabled = true;
_heldBodyAvatar.renderer.enabled = false;
Destroy(_heldObject);
Destroy(_heldBodyAvatar);
if (onRelease != null)
onRelease();
_heldObject = null;
}
private void ThrowObject()
{
ReleaseObject(() => _heldObject.rigidbody.AddForce(_playerCam.transform.forward * ThrowStrength, ForceMode.Impulse));
}
}
if you can't decide whether it's unityscript or c# then you're going to have a really hard time if you just take this and try to use it, regardless of any errors.
start with something simpler like one of the many tutorials in the learn section. you'll have an easier time...
Answer by Nanofication · Feb 12, 2016 at 03:38 PM
That is C#.
My recommendation for avoiding errors is to separate that one script into 2. You have 2 MonoBehaviours which will give an error even in C#.
Also, my 2 cents, I usually use little parts from an example code to see what it does instead of copying and pasting the entire thing. Copying/ Pasting usually gives me undesirable results and more headaches.
It is c# yes, i still get some errors but i will solve them. thank you
Your answer

Follow this Question
Related Questions
Set public gameobject by raycast hit target 1 Answer
my script was written but does not want to work 1 Answer
Scripting Sprites || URGENT HELP 0 Answers
Set Bool to false after seconds? 2 Answers