Attach player to moving platform 3d unity - unity3d

I am trying to make a player move with the moving rock, the player jumps on it and the rock takes her from a to b but right now the player is just sliding off the rock. this is my code for the rock:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerattach : MonoBehaviour
{
public GameObject Player;
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "movingrock")
{
Player.transform.parent = other.gameObject.transform;
}
}
void OnTriggerExit(Collider other)
{
Player.transform.parent = null;
}
}

Double check your colliders. OnTriggerEnter will only work if your player character has a collider on it that is set to a trigger. Otherwise you want to use OnCollisionEnter. You can have more than one collider on an object
Personally, I would give the rock a trigger collider, instead of the player, as well as a normal collider, that just about covers the standing area on the rock. You can then detect the player and change the parent of the player through that. That way you won't have the player getting parented to it if they touch the side of the rock or a different part of it

Related

How do I rotate an object's parent without the child being moved along?

In my game on unity 2d I have spaceship and a planet. The planet is orbiting a star so I made a script that parents the planet to the player when I get within a range so the planet doesn't fly past or into the player. This script makes the player move with the planet so they land on it and fly around it easily.
Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParentPlayer : MonoBehaviour
{
[SerializeField] GameObject Player;
private float Dist;
[SerializeField] float Threshold;
private CircleCollider2D ParentTrigger;
// Start is called before the first frame update
void Start()
{
ParentTrigger = GetComponents<CircleCollider2D>()[1];
ParentTrigger.isTrigger = true;
ParentTrigger.radius = Threshold / transform.localScale.x;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject == Player)
{
collider.gameObject.transform.SetParent(transform);
}
}
private void OnTriggerExit2D(Collider2D collider)
{
if(collider.gameObject == Player)
{
collider.gameObject.transform.SetParent(null);
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, Threshold);
}
}
The problem is that as the planet rotates it ends up moving the player that has been parented to it. How can I make the planet's rotation not affect the position and rotation of the player, but still make the planets position affect the position of the player?
This might not be what you're looking for but I'm going to add it reguardless. Given that the Child-object will follow the parent, I would suggest putting the planet and spaceship as a child of the empty gameobject. Then you could rotate the the planet object, and if planets are in movement, you could add the movement to the parent object.
How about instead of parenting the player, write a script to set its position to the planets + some offset, and then the rotation wouldn't be an issue. Alternatively, if the player doesn't rotate at all, add constraints to its Rigidbody. Maybe something like this:
player.transform.position = planet.transform.position + offset;
Hope that works!

Position of particlesystem isn't attached to object

I added a particle system to my unity project. But I can't make it attach to a certain game object or tag. How can I make a Particle System sit on the center of a cube, while it's getting played once if the player triggers with this cube?
Code used for the trigger to work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision_Player_grower : MonoBehaviour
{
public GameObject Player_grower;
public ParticleSystem CollisionGrower;
// Start is called before the first frame update
void Start()
{
times_player_grower = 0;
CollisionGrower.transform.position = Player_grower.transform.position;
}
// Update is called once per frame
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Player")
{
print("we hit an playergrower");
CollisionGrower.Play();
Destroy(Player_grower);
}
}
}
Note: It does work if I manually place the particle system in the heart of the cube, but I assume this can be done in an easier way.
From what I understand, you want the position of the particle system to be in the center of another object.
1)
You could do that simply by attaching a particle system game object as a child of the cube you were talking about.
2)
You could do this with code instead. In case you want the particle system to do something extra, you could just adjust the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision_Player_grower : MonoBehaviour
{
public GameObject ParticleSystemObject;
public GameObject Cube;
public GameObject Player_grower;
public ParticleSystem CollisionGrower;
// Start is called before the first frame update
void Start()
{
times_player_grower = 0;
CollisionGrower.transform.position = Player_grower.transform.position;
}
// Update is called once per frame
void Update()
{
ParticleSystemObject.transform.position = Cube.transform.position;
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Player")
{
print("we hit an playergrower");
CollisionGrower.Play();
Destroy(Player_grower);
}
}
}
I don’t fully understand what the rest of the variables are for, and what they are, so I may have done some un-needed things.
First, I added two GameObject variables. One of them is the cube object you want the particle system to go to, and the other is the particle system as a game object. I set the position of the particle system to the position of the cube.
Important: if you have this script attached to the cube, remove the Cube variable.
Instead of using
... = Cube.transform.position;
Use
... = transform.position;

How to call a method every time, when player stay at any collider?

I have a game like CubeSurfer. When player goes through an collectable, two methods are called (for "jumping" and creating a cube under player). But, when player goes through second, or third collectable, nothing happens. I tried to attached script to collectable, and tried to put the method "OnTriggerEnter" in the body of methods "Start" and "Update". But it doesn't work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCollider : MonoBehaviour
{
[SerializeField] Collider player;
[SerializeField] GameObject destroyIt;
[SerializeField] GameObject objToClone;
[SerializeField] Transform rootObjInScene;
Transform curParent;
Vector3 posOffset, posPlayer;
float distance = 1f;
private void OnTriggerStay(Collider player)
{
PlayerJumping();
CreateAnother();
Destroy(destroyIt);
}
public void PlayerJumping()
{
posPlayer = Vector3.up * distance;
Vector3 playerPos = rootObjInScene.position + posPlayer;
rootObjInScene.transform.position = playerPos;
}
public void Awake()
{
curParent = rootObjInScene;
posOffset = Vector3.down * distance;
}
public void CreateAnother()
{
Vector3 newPos = curParent.position + posOffset;
GameObject newObj = Instantiate(objToClone, newPos, Quaternion.identity, curParent);
curParent = newObj.transform;
}
}
You should also check your Rigibody collision detection mode in the inspector.
By default, this is set to Discrete, but if your object is fast-moving, you should set it to Continuous or ContinuousDynamic, otherwise the collisions won't be registered because the object is moving too quickly.
These are a little bit performance intensive, so I'd still recommreading up on the Unity documentation or digging through a couple of good articles to know what's best for you :)
You shouldn't call OnTriggerEnter explicitly (in your Update()), it's an event message and gets called automatically by unity when you enter a Collider marked isTrigger.
You can use OnTriggerEnter only if your collider is marked isTrigger, if you want to get a collision with a Collider that is not explicitly a trigger, you use OnCollisionEnter
To execute a method repeatedly while touching a Collider use OnCollisionStay, while touching or inside a Collider with isTrigger use OnTriggerStay
OnCollisionStay
OnTriggerStay
You can use OnCollisionStay or OnTriggerStay
void OnCollisionStay(Collision collisionInfo)
{
//Do something when collisionInfo.gameObject stay in this.gameObject collider
}

Unity2D - Ignore collision with edge collider

I am trying to have my player ignore the collision with an edge collider on a platform i have.
Here's the script that I have added to the player
public class TestMovement : MonoBehaviour
{
public Rigidbody2D ball;
private GameObject purplePlat1;
private GameObject player;
// Start is called before the first frame update
void Start()
{
purplePlat1 = GameObject.Find("purple_plat");
player = GameObject.Find("circle-png-44659");
ball = GetComponent<Rigidbody2D>();
ball.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
Debug.Log("start");
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D collision)
{
Physics2D.IgnoreCollision(purplePlat1.GetComponent<EdgeCollider2D>
(), GetComponent<CircleCollider2D>());
Debug.Log("collision");
}
}
The ball is still hitting the platform. I have confirmed that the oncollisionenter method is firing.
You can use the layer system of Unity to avoid collisions between both. Set a layer for a player and another for the edge and untick the collision between them.
What you can do is create a layer mask for the different type of game objects. Then, open your Physics2D settings.
On the bottom part, you can see a matrix of physics objects that can collide to one another. Just uncheck which layer should not collide with the other.

How to jump into a 2.5D world?

I'm totally new with Unity (and game development in general). I followed the great simple tutorial Survival Shooter and I have one question: in this tutorial, we add a Y constraint position to the rigidbody's character plus we set the drag value and the angular drag value to infinite. How can we make the character jump since those settings prevent the character from moving to the Y axis?
If someone can give me an hand on that please...
Thanks a lot!
Why exactly do you add the constraint on the Y axis? You could remove it, and then just add gravity that will make your player stick to the ground. After that just apply a force, or just a simple translation going upwards by a set speed, to make the player jump and then wait for gravity to bring him back down.
This is what I would do to jump.
P.S. you need to remove the constraints on the Y axis on the vector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public Vector3 force;
public Rigidbody rb;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space) && transform.position.y == 0) //Enter your y axix where ground is located or try to learn a little more about raycasting ill just use 0 for an example)
{
rb.AddForce(force);//Makes you jump up when you hold the space button down line line 19 will do so that you only can jump when you are on the ground.
} if (Input.GetKeyUp(KeyCode.Space))
{
rb.AddForce(-force); //When you realase the force gets inverted and you come back to ground
}
}
}
I would do this instead edit the code over this post.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Just : MonoBehaviour {
public Vector3 force;
public Rigidbody rb;
bool isGrounded;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) //Rember to got to your "Ground" object and tag it as Ground else this would not work
{
rb.AddForce(force);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
}
You need to assign your ground object a tag called Ground, You need to make your own tag called Ground its not that hard tho you click your object and top left of the inspector there's the tag and then u just make a new tag called Ground. And also plz rember to assign the other values on your player object.