I have this pretty simple script I’d say:
using UnityEngine;
using System.Collections;
public class BounceOnContact : MonoBehaviour
{
public float Bounce;
public string BounceAnimationName;
public AudioClip[] BounceSounds;
private Animator anim;
private AudioSource aSource;
void Start()
{
if (GetComponent<Animator>() != null)
{
anim = GetComponent<Animator>();
}
if (GetComponent<AudioSource>() != null)
{
aSource = GetComponent<AudioSource>();
}
else
{
aSource = gameObject.AddComponent<AudioSource>();
}
}
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.GetComponent<Rigidbody2D>() != null)
{
Rigidbody2D rBody = collisionInfo.collider.GetComponent<Rigidbody2D>();
Vector2 vel = rBody.velocity;
Vector2 normal = collisionInfo.contacts[0].normal;
Vector2 newDirection = Bounce * (-2.0f * (Vector2.Dot(vel, normal) * normal));
rBody.AddForce(newDirection, ForceMode2D.Impulse);
if (anim != null)
{
anim.Play(BounceAnimationName);
}
if(BounceSounds.Length > 0)
{
int index = Random.Range(0, BounceSounds.Length);
AudioClip audio = BounceSounds[index];
aSource.clip = audio;
aSource.Play();
}
}
}
}
The script itself makes whatever it’s attached to work as a spring. The script works just fine on PC and I want to build the project for Mobile. So, I borrowed a Mac computer so I can build for iOS. In order to do this I just copied the project folder and put it on the mac computer and opened it in Unity there. However, the spring now only half works.
The spring plays an animation and a sound like it’s supposed to but it doesn’t apply any force to the player object that hits the spring. I opened up the script in Monodevelop and tried to debug the script. I set a breakpoint and the code goes through exactly as it’s supposed to, but it doesn’t apply the force to the player object at all. This happens both on the Mac and iOS.
Any idea why?