I am working on a game where the sprite follows the mouse clicks. I need to add boxcolliders for scoring points and boundaries. I’ve tried void OnCollisionEnter (Collision col)
, and that doesnt work. Here is my code.
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public float speed = 1.5f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
var mouse = Input.mousePosition;
var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
transform.rotation = Quaternion.Euler(0, 0, angle);
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}
All i need is to change Transfor.position to Rigidbody.addforce:)