movement on object click - unity3d

On pressing down the object I want it to move continuously.I am a beginner in Unity 3D.
Please help.
function OnMouseDown()
{
Debug.Log("its a hit");
function Update()
{
transform.Translate(Vector3(0,0,3)*Time.deltaTime);
}
}

Check out this Unity Answer for several approaches on how to drag an object with the mouse:
http://answers.unity3d.com/questions/12322/drag-gameobject-with-mouse.html

Just click the mouse
function Update()
{
if(Input.GetMouseButtonDown(0)) //Left click
{
Debug.Log("Left Mouse Button Click");
transform.Translate(Vector3(0,0,3)*Time.deltaTime);
}
}
Or with select object
function Update()
{
if(Input.GetMouseButtonDown(0)) //Left click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, hit))
{
hit.collider.gameObject.Translate(Vector3(0,0,3)*Time.deltaTime);
}
}
}

Related

Unity 3D NavMesh Agent when hit breaks

When I hit the enemy(which has the navMeshAgent) with my player the enemy is sent to the corner of the map and fidgets. It is sent in the direction that the player pushes it in. I just want it to go to the player for now.
The code
{
GameObject playerPosition;
NavMeshAgent navMesh;
// Start is called before the first frame update
void Start()
{
playerPosition = GameObject.FindGameObjectWithTag("Player");
navMesh = GetComponent<NavMeshAgent>();
if(navMesh == null)
{
Debug.Log("Nav Mesh for enemy brock");
}
}
private void Update()
{
Vector3 dir = playerPosition.transform.position;
navMesh.destination = dir;
}
}
This is what it looks like

Gun should shoot only when button is touched instead of entire screen

I've made a 3d game and I have a first person controller, control by a joystick. I want my player to shoot only when I touch a button on the screen, but now my player shoots if I touch anything on the screen. This is the script for the shoot function:
using UnityEngine;
using UnityEngine.UI;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
public Button button;
// Start is called before the first frame update
void Start()
{
//Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
button.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
}
void Shoot()
{
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if(target != null)
{
target.TakeDamage(damage);
}
}
}
void OnCollisionEnter(Collision collision)
{
Debug.DrawRay(collision.contacts[0].point, collision.contacts[0].normal, Color.green, 2, false);
}
void TaskOnClick()
{
Shoot();
}
}
Your Shoot() method runs whenever you press the "Fire1" button, since the only check you are doing is if (Input.GetButtonDown("Fire1")).
If you want to depend on a button to fire you need to take this part of the code out and call Shoot() whenever you click the button.
You can add the event to the button using code, as explained here
public Button m_YourFirstButton;
void Start()
{
//Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
m_YourFirstButton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
//Output this to console when Button1 or Button3 is clicked
Debug.Log("You have clicked the button!");
}
or you could call it from the inspector by referencing your Gun monobehaviour.

How rotate camera to Mouse click

I make viewer of models (with Unity3d). Now I make mouse interaction with model. How rotate camera to mouse click position. http://prntscr.com/990q9y
Maybe something like this?
You want "click", right?
Oh, You want to change camera.
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Camera.main.transform.localEulerAngles=hit.point;
}
}
//or try to fix the z thing:
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Camera.main.transform.localEulerAngles = ray.origin + (ray.direction * 4.5f);
}
I see this here: http://answers.unity3d.com/questions/376735/get-world-coordinates-from-mouse-click.html
and maybe this can help you a lot: How to constrain rotation from mouse input in unity 5?
This code work, but how smoothly rotate?
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
transform.transform.LookAt(hit.point);
player.UpdatePosition();
}
}

buttons only react to clicks/touches when clicking/touching to the right of them

I'm making a 2D game in Unity3D for android. Right now I'm making buttons. And this buttons does not react clicks/touched properly. I've got same issue with mouse clicks and touches both. Every button has trigger boxcollider with a same size as an object. BUT buttons react only when I click on area, that is right from a button. I don't understand why is it so. What should I do? Here is my code:
if (Input.GetMouseButtonDown(0)) {
Vector3 i = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));
RaycastHit2D hit = Physics2D.Raycast (i, i);
if (hit.transform != null) {
if (hit.transform.tag == "button") {
hit.transform.gameObject.SetActive(false);
}
}
}
Also, I've instantiated an object on mouse click on "i" position to check does it convert screen position to world correctly, and it works fine.
the first parameter in Physics2D.Raycast is the origin and the second one is direction so you should make the raycast from your ray.origin in the direction of ray.direction
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
if (hit) {
if(hit.collider.gameObject.tag=="button"){
//do something
}
}
}
}
Try to handle it by this way:
if (Input.GetMouseButtonDown(0)) {
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 touchPos = new Vector2(pos.x, pos.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit) {
Debug.Log(hit.transform.gameObject.name);
if (hit.transform.tag == "button") {
hit.transform.gameObject.SetActive(false);
}
}
}

Unity hit.rigidbody reference to concrete gameObject

I've got Menu with 3d texts. Main Camera has got animations that I want to play after clicking on text. Every text has its own rigidbody and collider. I made this code:
#pragma strict
import UnityEngine;
var object : GameObject;
var Run : AnimationPlay;
function start(){
Run = object.GetComponent(AnimationPlay);
}
function Update() {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
if (hit.rigidbody != null){
Debug.Log("Start Game pushed");
Run.action = true;
}
}
}
}
for Start Game button and this one:
#pragma strict
import UnityEngine;
var object : GameObject;
var Run : AnimationPlay2;
function start(){
Run = object.GetComponent(AnimationPlay2);
}
function Update() {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
if (hit.rigidbody != null){
Debug.Log("Back pushed");
Run.action2 = true;
}
}
}
}
for Back button.
The problem is it's no matter on which rigidbody I will click, both animations are starting to play and that makes weird effects. I don't know how to make a reference to concrete gameObject's rigidbody.
I solved my problem. I replaced
hit.rigidbody != null
with
hit.rigidbody.gameObject.tag == "StartGame" //object's tag here
and now it works fine.