Hello i'm trying build a game i have a problem, i have an object i put a ball over it now when i set the object move up and down the ball can't let the object to move up, i want the ball not effect the object and go with it up or down or right or left
public class PipeController : MonoBehaviour {
Rigidbody2D rb;
[SerializeField] float speed;
[SerializeField] float maxY;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
PipeMove();
}
// Update is called once per frame
void Update () {
}
void PipeMove()
{
rb.velocity = new Vector2(speed, maxY);
}
Add a rigid body to the ball. The object can't move because it has an static object above.
Related
So, I'm trying to have the camera follow the player (a car) and rotate with the player (... a car). However, it seems that either the quaternion.lerp is sus, or the positioning is. I've tried localPosition, but it doesn't really have an effect. It's rotating with the player, but it stays like... on one side of the player. Here is what I mean:
https://imgur.com/a/TQJOQ2X
and then
https://imgur.com/a/4FKm709
here's the code:
public class CameraMove : MonoBehaviour
{
[Header("References")]
[SerializeField] Transform player;
[Header("Attributes")]
[SerializeField] float camDelayIntensity = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.localPosition = player.position + new Vector3(0, 3, -8);
Quaternion endPos = Quaternion.LookRotation(player.forward);
transform.rotation = Quaternion.Lerp(transform.rotation, endPos, camDelayIntensity);
}
}
The object is not moving down the slope i have also tried to reduce the friction to zero but not working and i am using a ball as my object
how to move an object in unity in 2d
Here is my code:
public class move : MonoBehaviour
{
public float moveSpeed = 3f;
Rigidbody2D rig;
float yat ;
void Start()
{
rig = GetComponent<Rigidbody2D>();
}
void Update()
{
float xat = Input.GetAxis("Horizontal");
yat = rig.velocity.y;
rig.velocity = new Vector2(xat*moveSpeed,yat);
}
}
My apologize i don't get it exactly what you try to do? But if you simply moving the object you can try "Transfrom.Translate" method.
By the way, using velocity method try this code in your own version :
float speed = 0.5f;
...
void Update () {
float inputHorizontal = Input.GetAxisRaw("Horizontal");
player.velocity = Vector2.right * inputHorizontal * speed;
}
Imo this should work, if you have any other query let me know.
I make a 3d game.My gun only shoot the ground , even when I select other object to shoot. I have the ground at a rotation , it may affects the game? Should I add something to the main camera?
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
}
}
}
picture ground
So I have a 2d gameObject that behaves likes a spike trap that springs out of the ground when the character collides on the trigger. I use AddForce to the rigidbody 2D of the gameObject to manipulates its speed when coming out of the ground and I want it to just sticking out of the ground. How can I stop it when it reaches a certain tranform Y value.
Here is my code:
public float speed;
Rigidbody2D rb;
void Start () {
rb = GetComponent<Rigidbody2D>();
}
void Update () {
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
rb.AddForce(new Vector2(0, speed * Time.time), ForceMode2D.Impulse);
}
}
Certain transform value, or certain transform.position value?
I'll do it like this:
private float threshold = 10f;
private float startPosition = 0;
private RigidBody2D pikeRigidbody;:
private void Start()
{
startPosition = = this.transform.position;
pikeRigidbody = this.GetComponent<RigidBody2D>();
}
private void Update(){
if(this.transform.position.y >= (startPosition.y + threshold))
{
pikeRigidbody.velocity = Vector3.zero;
}
}
And attach the script to the pike object.
Edited to position.y instead of position
When i shoot my bullet it chases the enemy. I want to have an aiming system,
for example, the bullet should go to the enemy's position when it was first seen (not chasing object, just shoot to the first position the enemy was seen).
This is my code and it makes the bullet follow the enemy:
void shoot() {
GameObject bulletGO=(GameObject) Instantiate(BulletPrefab, firepoint.position, firepoint.rotation);
Bullet bullet = bulletGO.GetComponent<Bullet>();
if (bullet != null) {
bullet.Seek(target);
}
and :
private Transform target;
public float speed = 5f;
public GameObject ImpactEffect;
public void Seek(Transform _target) {
target = _target;
}
// Update is called once per frame
void Update () {
if (target == null)
{
Destroy(gameObject);
return;
}
Vector3 dir = target.position - transform.position;
float distancethisframe = speed * Time.deltaTime;
if (dir.magnitude <= distancethisframe)
{
HitTarget();
return;
}
transform.Translate(dir.normalized * distancethisframe, Space.World);
}
so any idea?
Replace
private Transform target;
//...
public void Seek(Transform _target) {
target = _target;
}
//...
Vector3 dir = target.position - transform.position;
with
private Vector3 target;
//...
public void Seek(Transform _target) {
target = _target.position;
}
//...
Vector3 dir = target - transform.position;
That way you caluclate and copy existing target position in Seek and make bullet fly to that position, even if actual target move away. The code you had actually took current target position each time the Update was called.