Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ahgr123 · Feb 12, 2016 at 03:28 PM · scripting beginnerscriptingbasics

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));
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Feb 12, 2016 at 03:37 PM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ahgr123 · Feb 12, 2016 at 04:20 PM 0
Share

It is c# yes, i still get some errors but i will solve them. thank you

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

35 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to block one input while another is in use? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges