I have a target object (cube), and a fake mouse that I created from an object (sphere) to be controlled by gamepad joystick. I want to bring out another object (let say; sphere), when I push the gamepad button and precisely hit the target object (cube).
Before, I have tried with a mouse click, and it succeeds, but when I controlled the fake mouse with a joystick, when I press the button even outside the target (cube) it still brings out another object. Here is the code, if anybody can help me to revise it. Thanks
function Start () {}
function Update () {
if (Input.GetButtonDown ("Fire1"))
{
var Cube = GameObject.FindGameObjectsWithTag ("Cube");
if (Cube[0].GetComponent(MeshRenderer).enabled){
var Circle1 = GameObject.FindGameObjectsWithTag ("Circle1");
Circle1[0].GetComponent(MeshRenderer).enabled = true;
Circle1[0].GetComponent(MeshRenderer).material.color = color.red;
}
}
}
[solved]
Thanks to EmreE and Draco18s for replying my question. I have solved it, I made a collision trigger indeed.
Here is my code after several trials.
if (Input.GetButtonDown ("Fire1") && isCollide)
{
var Cube = GameObject.FindGameObjectsWithTag ("Cube");
if (Cube[0].GetComponent(MeshRenderer).enabled){
var Circle1 = GameObject.FindGameObjectsWithTag ("Circle1");
Circle1[0].GetComponent(MeshRenderer).enabled = true;
Circle1[0].GetComponent(MeshRenderer).material.color = Color.red;
Debug.Log("Muncul");
}
}
function OnTriggerEnter (col : Collider)
{
Debug.Log(isCollide);
if(col.gameObject.name == "Mouse3DSphere")
{
isCollide = true;
}
}
function OnTriggerExit (col : Collider)
{
Debug.Log(isCollide);
if(col.gameObject.name == "Mouse3DSphere")
{
isCollide = false;
}
}
Related
I'm trying to make a pickup system and I thought it would be cool to do an outline around the item when you're looking at it. The issue I'm facing though is when you're no longer looking at the object I need to disable the outline. I ended up doing an odd solution and would like to get some help improving it.
public class PlayerCamera : MonoBehaviour
{
public Transform playerBody;
public Transform cameraHolder;
public float sensitivity;
public float currentY;
void Update()
{
MoveCamera();
LookingAtObject();
}
Outline objectOutline;
void LookingAtObject()
{
if(Physics.Raycast(cameraHolder.transform.position, cameraHolder.transform.forward, out var hit, Mathf.Infinity))
{
var obj = hit.collider.gameObject;
var outline = obj.GetComponent<Outline>();
if (obj && outline)
{
objectOutline = hit.transform.GetComponent<Outline>();
if (objectOutline)
objectOutline.OutlineWidth = 7;
}
else if (objectOutline)
objectOutline.OutlineWidth = 0;
}
}
}
You can store the outlined object in a variable, and whenever you hit a different outline object or hit nothing, set the outline back to zero.
Outline objectOutline;
void LookingAtObject()
{
if (Physics.Raycast(...))
{
var outline = hit.collider.GetComponent<Outline>();
// Make sure the hit object is not the same one we already outlined
//
if (outline != objectOutline)
{
// Remove the outline from our previously viewed object
//
if (objectOutline != null)
{
objectOutline.OutlineWidth = 0;
}
// Store the new outline object
//
objectOutline = outline;
// Since outline could be null, we need to check null before outlining
//
if (objectOutline != null)
{
objectOutline.OutlineWidth = 7;
}
}
}
// If we have an object we outlined and we didnt hit anything,
// remove the outline and reset the variable
//
else if (objectOutline != null)
{
objectOutline.OutlineWidth = 0;
objectOutline = null;
}
}
You need two events to solve the problem. Input frame and ray output frame. This code detects which raycast event is by recording the previous raycastHit frame and comparing it to the current hit, and sets the outline accordingly.
private RaycastHit lastHit;
void Update()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out var hit);
//Physics.Raycast(cameraHolder.transform.position, cameraHolder.transform.forward, out var hit, Mathf.Infinity);
if (hit.transform != lastHit.transform)
{
if (hit.transform) // when raycast Begin
{
var outline = hit.transform.GetComponent<Outline>();
outline.OutlineWidth = 7;
}
else if (lastHit.transform) // when raycast out
{
var outline = lastHit.transform.GetComponent<Outline>();
outline.OutlineWidth = 0;
}
}
lastHit = hit;
}
Hint: I commented on your raycast code for testing. If you want to change the raycast code as before.
In my level, I have a water collider where if you fall in, it triggers a splash effect and water sounds. However, because there is already an object inside the water, whenever I start the level, the water collider triggers and splash and water sounds despite the object already being in the collider.
So, even with the object deep inside the water collider, it creates the splash sound and water effect as if it just fell in.
How do I prevent this?
My code involves OnTrigger2D functions. But how do I make Unity check if an object is already colliding before level load?
Code:
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
gravityoriginal = playerrigidbody.gravityScale;
massoriginal = playerrigidbody.mass;
playerrigidbody.gravityScale = 0.1f;
playerrigidbody.mass = other.GetComponent<Rigidbody2D>().mass + 2f;
splash.Play(); //Plays the initial splash if velocity is high
underwaterbool.IsUnderwater = true; //stop dust particle
mainmusic.enabled = true;
powerupmusic.enabled = true;
deathmusic.enabled = true;
}
else if (other.tag == "Snatcher")
{
masssnatcheroriginal = snatcherrigidbody.mass;
gravityoriginalsnatcher = snatcherrigidbody.gravityScale;
snatcherrigidbody.gravityScale = 0.1f;
snatcherrigidbody.mass = other.GetComponent<Rigidbody2D>().mass + 2f;
splashsnatcher.Play();
snatchersounds.enabled = true;
}
else if (other.tag != "Player" && other.tag != "Snatcher" && other.GetComponent<Rigidbody2D>() != null)
{
gravityoriginalbox = other.GetComponent<Rigidbody2D>().gravityScale;
massoriginalbox = other.GetComponent<Rigidbody2D>().mass;
other.GetComponent<Rigidbody2D>().mass = other.GetComponent<Rigidbody2D>().mass + 2f;
other.GetComponent<Rigidbody2D>().gravityScale = 0.1f;
other.GetComponent<ParticleSystem>().Play(false);
splashaudio.Play();
Splashparticlesforbox.IsUnderwaterBox = true;
}
if(other.GetComponent<Rigidbody2D>() != null)
{
other.GetComponent<Rigidbody2D>().velocity = new Vector2(0f, -0.5f);
}
if (!cooldown)
{
splashaudio.Play();
}
cooldown = true;
StartCoroutine(waittime());
}
Can you post your OnTrigger2D functions code please? Normally Unity does not trigger the OnTriggerEnter method if an object is already inside the trigger when the scene beggins but the OnTriggerStay is executed every frame.
Anyway... one option (not the best one I think) would be to put a boolean propoerty in the trigger that is initialized true and use it to prevent the OnTriggerFunctions to do anything til the fame ends. Then in the LateUpdate method you can set the property as false.
bool m_FirstFrame = true;
void onEnable()
{
m_FirstFrame = true;
}
void OnTriggerEnter2D(Collider2D collision)
{
if(m_FirstFrame){
return;
}
.... //Rest of code
}
//Same for the other OnTrigger2D methods you use
void LateUpdate()
{
m_FirstFrame = false;
}
I hope it helps! Tell me if you need something more, and please, post your code, that way is easier for us to fint where is the issue and get how to fix it.
Good luck ^^
I am looking to show a line in my app from where the model is placed so that the user knows position where the model is kept in real world. When user changes device camera away from model the line gets turned on to show where the model is. Similarly it turns off when model is detected. I have attached images to show from a similar app white dotted lines show the path. Notice how the lines disappear when the model is detected.
LineRenderer lins;
public GameObject Lineprefab;
private GameObject newline;
public Transform startpoint;
public Renderer m_rend1;
bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
{
List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);
if (hitResults.Count > 0 && check==true)
{
foreach (var hitResult in hitResults)
{
Debug.Log ("Got hit!");
//obj.Hideplane();
Genplanes.SetActive(false);
if (Select == 0) {
Debug.Log("hit-zero!");
Instantiate(Instaobj[0], ForSelect);
check = false;
}
if (Select == 1) {
Debug.Log("hit-one!");
Instantiate(Instaobj[1], ForSelect);
check = false;
}
if (Select == 2) {
Debug.Log("hit-two!");
Instantiate(Instaobj[2], ForSelect);
check = false;
}
m_HitTransform.position = UnityARMatrixOps.GetPosition (hitResult.worldTransform);
m_HitTransform.rotation = UnityARMatrixOps.GetRotation (hitResult.worldTransform);
Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
obj.StopPlaneTracking();
}
}
return false;
}
private void Start()
{
spawngenerator();
newline.SetActive(false);
m_rend1 = GetComponent<MeshRenderer>();
}
void spawngenerator()
{
GameObject newline = Instantiate(Lineprefab);
lins = newline.GetComponent<LineRenderer>();
}
private void LateUpdate()
{
lins.SetPosition(0, startpoint.position);
lins.SetPosition(1, m_HitTransform.position);
if( m_rend1.isVisible==true)
{
Debug.Log("Render is Visible");
newline.SetActive(false);
}
else if( m_rend1.isVisible==false)
{
newline.SetActive(true);
Debug.Log("It is InVisible");
Debug.Log("Render is InVisible");
}
}
void Update () {
#if UNITY_EDITOR //we will only use this script on the editor side, though there is nothing that would prevent it from working on device
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
//we'll try to hit one of the plane collider gameobjects that were generated by the plugin
//effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
if (Physics.Raycast (ray, out hit, maxRayDistance, collisionLayer)) {
//we're going to get the position from the contact point
m_HitTransform.position = hit.point;
Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
//and the rotation from the transform of the plane collider
m_HitTransform.rotation = hit.transform.rotation;
}
}
#else
if (Input.touchCount > 0 && m_HitTransform != null )
{
var touch = Input.GetTouch(0);
if ((touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
ARPoint point = new ARPoint {
x = screenPosition.x,
y = screenPosition.y
};
// prioritize reults types
ARHitTestResultType[] resultTypes = {
//ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry,
ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
// if you want to use infinite planes use this:
//ARHitTestResultType.ARHitTestResultTypeExistingPlane,
//ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane,
//ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane,
//ARHitTestResultType.ARHitTestResultTypeFeaturePoint
};
foreach (ARHitTestResultType resultType in resultTypes)
{
if (HitTestWithResultType (point, resultType))
{
return;
}
}
}
}
#endif
}
.
First, I 'd start with checking if the model is within the bounding box of the camera https://docs.unity3d.com/ScriptReference/Renderer-isVisible.html
if the object is not visible (isVisible == false), create a line renderer from object position to wherever it should end.
The end point could be a camera child place just in front of it, so it looks like it starts from the user to the object.
i have tried everything to get the destroy to work Destroy(enemytrans) Destroy(enemy) Destroy(enemy.gameObject..... ect all abbreviations wont work.
can someone please help ???
i can coll with all objects and they do destroy but if 2 enemies are on screen i hit the one closest to me the other one will die because it was last spawned how can i destroy the one i jump on ?? mind efukd. :(
var Player : GameObject;
var Gravity:float = 2;
var speed:float = 2;
var enemytrans : Transform;
var enemy: GameObject;
public var jumped = false;
var anim : Animator;
function Start () {
while (true) {
yield WaitForSeconds (Random.Range(3, 0));
enemy = Instantiate(enemytrans).gameObject;
}
anim = GetComponent(Animator);
// Instantiate the prefab and put the gameObject into "theCube"
}
function Update () {
Player.transform.position.x = -4.325;
//the gravity function
if (jumped == false){
anim.SetFloat("hf",0.0);
}
if (Input.GetButtonDown("Fire1") && jumped==false){
fire();
jumped = true;
}
if(jumped==true){
anim.SetFloat("hf",1);
}
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("ground")){
anim.SetFloat("hf",0.0);
jumped=false;
}
if(coll.gameObject.CompareTag("enemy") && jumped==true){
fire();
jumped=true;
Destroy(enemy,1);***********************************************this line************
}
if(coll.gameObject.CompareTag("enemy") && jumped==false){
Destroy(Player);
}
}
function fire(){
Player.transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
Player.rigidbody2D.velocity = Vector2(0,10);
}
Problem
You are destroying the last spawned GameObject, which you keep a reference to in the enemy variable.
Solution
You should destroy what you hit. Unity3D already gives you who you collided against so just use that information.
function OnCollisionEnter2D(coll: Collision2D)
{
if(coll.gameObject.CompareTag("enemy") && jumped==true)
Destroy(coll.gameObject);
}
I have a bomb and want it to explode when touched. I just tried implementing this with ray-casting but something isn't working. I'm using unity 2d settings.
Also I'm programming this on a computer(of course) so is there some setting I have to set to make it recognize mouse clicks as touches?
#pragma strict
var explosion:GameObject;
function Update () {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
var pos:Vector3 = Camera.main.ScreenToWorldPoint (Input.mousePosition);
var hitInfo:RaycastHit2D = Physics2D.Raycast(pos, Vector2.zero);
if (hitInfo != null && hitInfo.collider != null) {
Debug.Log ("I'm hitting "+hitInfo.collider.name);
var whatsHit:GameObject = hitInfo.collider.gameObject;
if(whatsHit.CompareTag("bomb")){
whatsHit.GetComponent(BombScript).Explode(whatsHit.transform.position);
}
} else {
Debug.Log("hitting nothing");
}
}
}
}
function Explode(pos:Vector3){
GameObject.FindGameObjectWithTag("GameController").GetComponent(BombSpawner).spawnBomb = true;
Instantiate(explosion, pos, Quaternion.identity);
Destroy (this.gameObject);
}
The OnMouseDown actually gets raised on touch as well. It, of course, limits your ability to track the finger Id, but if you don't need it then this is perfect since it does allow you to use it on both PC and touch devices with no extra code.