Hi all,
I have just recently started coding in c# on unity and am stuck on this. I need to use the left and right arrow keys to change the linear drag of an object that collides with the object I am working on. I am able to change the drag of the other object when it first collides with this object but I cannot use the arrow keys for this. This is the code I have so far. This is a 2D game by the way so the Rigidbody is 2D and the marblePlayer is a circle not a sphere.
using UnityEngine;
using System.Collections;
public class Platform1 : MonoBehaviour {
public GameObject marblePlayer;
public float modifyDrag;
public float oldDrag;
private bool ifCollision;
private float decelartionValue;
private float newDrag;
void Start() {
ifCollision = false;
decelartionValue = 2.0f;
}
void OnCollisionEnter2D(Collision2D collision) {
oldDrag = collision.rigidbody.drag;
collision.rigidbody.drag = modifyDrag;
ifCollision = true;
}
void OnCollisionExit2D(Collision2D collision) {
collision.rigidbody.drag = oldDrag;
ifCollision = false;
}
void update() {
if (ifCollision==true Input.GetKey(KeyCode.LeftArrow)) {
newDrag=marblePlayer.rigidbody.drag * decelartionValue;
marblePlayer.rigidbody.drag=newDrag;
}
}
}