- Home /
This question was
closed Sep 29, 2013 at 09:23 AM by
Fattie for the following reason:
Duplicate Question
Question by
Sphyx · Sep 29, 2013 at 01:48 AM ·
c#conversion
JS to C#, anyone?
Im having trouble converting the following code... Any help is very appreciatted!
#pragma strict
var colliding : Collider;
var body : Rigidbody;
var forceMultiplier = 25;
function OnCollisionEnter (collisionInfo : Collision) {
if (collisionInfo.gameObject.name == "Ball") {
var contactNormal = collisionInfo.contacts[0];
var rot = Quaternion.FromToRotation(Vector3.up, contactNormal.normal);
var pos = contactNormal.point;
var rv = collisionInfo.relativeVelocity.magnitude;
direction = body.transform.position - pos;
reflectBallTo = Vector3.Reflect (pos, direction);
colliding.gameObject.rigidbody.AddForce (reflectBallTo * forceMultiplier);
}
}
Comment
Best Answer
Answer by tw1st3d · Sep 29, 2013 at 03:48 AM
Thiiiis should work, might have a few minor bugs. But honestly, I only did this because I had nothing better to do. If it doesn't work, spend time fixing it to work before going any further with C#. That's how you learn, fix stuff up.
using UnityEngine;
using System.Collections;
using System;
public class BallCollide : MonoBehavior
{
public Collider colliding;
public RigidBody body;
public float forceMultiplier = 25;
public void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.gameObject.name == "Ball")
{
string contactNormal = collisionInfo.contacts[0];
Quaternion rot = Quaternion.FromToRotation(Vector3.up, contactNormal.normal);
// This might supposed to be something else, but I'm guessing it's a Quaternion.
float pos = contactNormal.point;
var rv = collisionInfo.relativeVelocity.magnitude;
// I have no idea what this is supposed to be, which I'm kicking myself for.
var direction = body.transform.position - pos;
// Again, no idea. Might be a float[]
Vector3 reflectBallTo = Vector3.Reflect (pos, direction);
colliding.gameObject.rigidbody.AddForce (reflectBallTo * forceMultiplier);
}
}
}
I mannaged to do something next to it... thanks for the help!
Follow this Question
Related Questions
Need help converting js to C 1 Answer
Need help converting js to C# - rigidbody.rotation.z 1 Answer
Need help C# -- yield, destroy 1 Answer
Multiple Cars not working 1 Answer
Need help converting js to C 2 Answers