How to make 3D object climb up wall in Unity 3D - unity3d

I am trying to make a 3D capsule climb up along an object I tagged as "Wall". However, when I make contact with the wall, It can't seem to rise along the wall. How would this be fixed?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallClimb : MonoBehaviour
{
public float riseSpeed;
public Rigidbody rb;
private void start()
{
riseSpeed = 5.0f;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
Debug.Log("Position: " + transform.position);
rb.velocity = transform.up * riseSpeed;
Debug.Log("Wall Hit");
}
}
}

Maybe when you are pressing 'w' there are 2 forces acting on the wall 1 is upward and 1 in direction of the wall, and the force in direction of the wall won't let you go upwards, also make sure there is no friction on the wall.

Related

Shooting bullets in unity2D

I'm trying to shoot bullets in a 2D top-down-shooter game but they just stay rest and doesnt move
I tried to add "addforce" but they didnt move. I changed it with "new Vector2" but that time it doesnt follow my cursor. istead of it shoots in a different same direction.I want to shoot to my cursor. and if possible I want to add a fire delay between object and make it possible to attack repeatly while holding mouse left button.
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 15f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
Shoot();
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab,firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right* bulletForce, ForceMode2D.Impulse);
}
}//class
`

Trying to make a VR fps game in Unity, but my gun is randomly inaccurate

Newbie to unity and vr here.
I recently got started with Valem's unity tutorial for VR, but I've hit a snag:
In his part 5 video where he explains how to pick up and interact with objects, the code he uses for the gun results in the bullets being very inaccurate when I used it.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.XR;
public class Gun : MonoBehaviour
{
public float speed = 40;
public GameObject bullet;
public Transform barrel;
public AudioSource audioSource;
public AudioClip audioClip;
public void Fire()
{
GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
spawnedBullet.GetComponent<Rigidbody>().AddForce(transform.forward * speed);// = speed * barrel.forward;
audioSource.PlayOneShot(audioClip);
Destroy(spawnedBullet, 2);
}
}
In his video his gun fires accurately, but for me it randomly shoots in a different direction (usually forward but rarely straight).
Really need help, I've been hitting my head against this one all day.

why does my raycast sometimes shoot in the wrong direction?

I'm trying to shoot a raycast from a gameobject at the front of a gun that is being carried by an XR hand. The gun shoots fine most of the time, but sometimes, for some reasons beyond my fragile little minds comprehension, it shoots in the wrong direction. It shoots in random directions, but never more than 90 degrees of where it should be. This is my code. [I have attached a video of it happening.][1] Every time I think I find a pattern, it changes. All I can say is that I have reason to believe it's related to mesh colliders. Only very vague reason though. Edit: https://youtu.be/JxC4wq9nVRw here's an updated video, it shows the problem more clearly. I also changed the redundant if statements.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GunBasic : MonoBehaviour
{
public Transform BulletShootPoint;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public AudioSource audioSource;
public AudioClip audioClip;
public float Ammo;
public TextMeshPro text;
public void Fire()
{if (Ammo > 0)
{
muzzleFlash.Play();
audioSource.PlayOneShot(audioClip);
RaycastHit hit;
if (Physics.Raycast(BulletShootPoint.transform.position, BulletShootPoint.transform.forward, out hit) && Ammo > 0)
{
Debug.Log(hit.transform.name);
GameObject impactDestroy = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactDestroy, 2f);
}
Ammo -= 1f;
}
}
private void Update()
{
text.text = Ammo.ToString();
}
public void Reload()
{
Ammo = 10;
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "ReloadTag")
{
Ammo = 10f;
}
}
}
It was a bug in the xr interaction toolkit. It was fixed by an update. Sorry I didn’t try this sooner, thanks for all the help.

Unity: Distance between two game-objects in the scene view

I am new to Unity and as I am creating my interface, I am trying to find what's the distance between two game objects. I know how to do in a C#/javascript script, however I am strugling to find this information in the scene view. Any key I could press while moving an object to see the distance to its neighbours ?
In a script, you can use Vector3.Distance between vector3 to get the distance between two points. Every gameObject has a position that is represented in a Vector3.
Below is a script example that shows you how it works. You just have to drag the script onto a gameObject in your scene and drag in the inspector another gameobject in your scene. The script should run even when your not in play mode because of the [ExecuteInEditMode].This way you will see the distanceBetweenObjects update in real time without actually having to hit play.
using UnityEngine;
[ExecuteInEditMode]
public class DistanceBetweenTwoObjects : MonoBehaviour
{
public GameObject obj;
public float distanceBetweenObjects;
private void Update()
{
distanceBetweenObjects = Vector3.Distance(transform.position, obj.transform.position);
Debug.DrawLine(transform.position, obj.transform.position, Color.green);
}
private void OnDrawGizmos()
{
GUI.color = Color.black;
Handles.Label(transform.position - (transform.position -
obj.transform.position)/2, distanceBetweenObjects.ToString());
}
}
The method OnDrawGizmos will draw text in between the 2 objects showing the distance value to help make it more user friendly.
There isn't any built-in functionality for this, but it's fairly trivial to add such a readout into the display using [ExecuteinEditMode], which causes scripts to run even when the game is not in play mode. This script, for example, should readout the distances on different axes and in total:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class EditorDistanceDisplay : MonoBehaviour
{
public GameObject target1;
public GameObject target2;
public float distanceX;
public float distanceY;
public float distanceZ;
public float distanceTotal;
void Start()
{
}
void Update()
{
Vector3 delta = target2.transform.position - target1.transform.position;
distanceX = delta.x;
distanceY = delta.y;
distanceZ = delta.z;
distanceTotal = delta.magnitude;
}
}

Programmatically attach two objects with a hinge joint

I have a ball that is attached to the bottom of a bar with a hinge joint that is swinging back and forth. The hinge joint is on the ball and attaches to the bar. When I tap the screen the hinge joint on the ball is destroyed and depending on the position of the ball will throw it using the built in physics.
I want to collide with another swinging bar and attach to it with a hinge joint. So basically a swinging game for a ball where you swing from bar to bar.
I can destroy the hinge joint just fine. How do I reattach it to the object it has collided with? I have the following:
Attach ball:
using UnityEngine;
using System.Collections;
public class attachBall : MonoBehaviour {
public GameObject player;
void OnTriggerEnter2D(Collider2D collider) {
if (collider.tag == "Player")
{
print ("COLLISION DETECTED!");
player.AddComponent<HingeJoint2D>();
}
}
}
Release ball:
using UnityEngine;
using System.Collections;
public class releaseBall : MonoBehaviour {
public GameObject player;
void Update(){
if (Input.GetMouseButtonDown (0)) {
Destroy(player.GetComponent("HingeJoint2D"));
}
}
}
I have placed a 2d circle collider on both the ball and the bar it is to collide with. It has a collision but when it does it locks the ball in place. I want it to attach to the bar it has collided with and swing with it.
Figured it out.
I had the script on the rope originally but I moved it to the ball.
Then I find the gameobject with the tag "Player" to re-attach the hinge joint to the ball.
Then I defined the object that I collided with and place it in the GameObject "rope".
Then I had to define the hinge I added and the rigidbody already attached to the rope.
After setting it as the connectedBody I moved the connected anchor to be on the end of the rope.
And voila. Ball swings on the rope.
using UnityEngine;
using System.Collections;
public class attachBall : MonoBehaviour {
public GameObject player;
public GameObject rope;
public HingeJoint2D hinge;
public Rigidbody2D rb;
public bool attached = false;
void OnTriggerEnter2D(Collider2D collider) {
if (collider.tag == "rope" && !attached)
{
Debug.Log ("collision");
attached = true;
player = GameObject.FindGameObjectWithTag ("Player");
player.AddComponent<HingeJoint2D> ();
rope = collider.gameObject;
hinge = player.GetComponent<HingeJoint2D>();
rb = rope.GetComponent<Rigidbody2D>();
hinge.connectedBody = rb;
hinge.connectedAnchor = new Vector2(0,-2.5f);
}
}
}