How to get rid of the clone item that stick to my main object?

Hi there,

I need help in getting rid of the clone object that has been sticking to my main object.

Here’s my code,

if (count>0)
{

if (Input.GetKeyDown (“space”))
{
Instantiate (item, shit.position, shit.rotation);
count = count - 1;
}
}

As you can see, my main object which is the blue cube sticks to the capsule which is the thing that I clone. But I want it to create the capsule when i pressed the space button. As you can see, there’s a counter in my code, which means that everytime when the cube picks up the yellow small cubes, it will gain one charge to allocate one capsule by pressing “space” key.

Thanks!

You are instantiating your object and assigning its position to be the same as your cube, from what I gather. Do you have any more code to show?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

public float speed;
public GameObject item;
public Transform shit;
private Rigidbody rb;

private int vertical, horizontal;
private int count = 0;
void Start ()
{
rb = GetComponent ();

vertical = 1;
horizontal = 0;

//item.SetActive (false);
}

void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag (“Pick Up”))
{
other.gameObject.SetActive (false);
count = count + 1;
//SetCountText ();
}
}

void Update ()
{
/*
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

if ((transform.position.x >= 10f && moveHorizontal > 0) || (transform.position.x <= -10f && moveHorizontal < 0))
{
moveHorizontal = 0;
}

if ((transform.position.z >= 10f && moveVertical > 0) || (transform.position.z <= -10f && moveVertical < 0))
{
moveVertical = 0;
}

Vector3 movement = new Vector3 (moveHorizontal * Time.deltaTime, 0.0f, moveVertical * Time.deltaTime);

print (moveHorizontal + “and” + moveVertical + “position” + transform.position);

//rb.MovePosition (transform.position + movement * speed);
transform.Translate (movement * speed);
//transform.Rotate (0, moveHorizontal, 0);
*/

if (Input.GetKeyDown (“up”))
{
vertical = 1;
horizontal = 0;
}
if (Input.GetKeyDown (“down”))
{
vertical = -1;
horizontal = 0;
}
if (Input.GetKeyDown (“left”))
{
vertical = 0;
horizontal = 1;
}
if (Input.GetKeyDown (“right”))
{
vertical = 0;
horizontal = -1;
}

if ((transform.position.z >= 10f && horizontal == 1) || (transform.position.z <= -10f && horizontal == -1))
{
horizontal = 0;
}

if ((transform.position.x >= 10f && vertical == 1) || (transform.position.x <= -10f && vertical == -1))
{
vertical = 0;
}

print (vertical + " and " + horizontal + " and " + transform.position.x);

Vector3 movement = new Vector3 (vertical * Time.deltaTime, 0.0f, horizontal * Time.deltaTime);
rb.MovePosition (transform.position + movement * speed);

if (count>0)
{

if (Input.GetKeyDown (“space”))
{
Instantiate (item, shit.position, shit.rotation);
count = count - 1;
}
}

}

}

Here’s the whole coding for the player. My game basically tells about a worm that eat up food and for each food it consumes, it will be able to gain one charge to spit out faeces on its location. But i do not want the faeces to stick to my main character which is the blue cube. The capsule represents the faeces.

Then I suggest one of the following:

Either create a child gameObject within your character object which is slightly offset from your character and use that as an “entry” point of the object to spawn or do it programatically.

Hi, Thanks Malleck.

Do you mean that I create a child object which is smaller, into my character? Currently, the capsule is already the child of the character, that is why the capsule overlaps the cube as my cube moves.Did you mean by using a smaller capsules to represent the feces, i will solve the problem? The cube and the capsule are a temporary representation of the Worm and the Feces. I will need to put the Worm and Feces later on, after the modelling is done in Blender. I am sorry if I have misunderstood what you’ve suggested since I am still new to this Unity software.

No problem.

What I mean is…

  • Create an empty gameObject as a child of your player gameObject
  • Transform this empty gameObject so that it is outside the bounds of your player character i.e. move the gizmo in the scene view to the butt-end of your character.
  • When you instantiate your gameObject, use that gameObject’s position as a spawn point.

3022340--225818--upload_2017-4-6_19-15-10.jpg

thank you very much!..now it works! brilliant!

3022340--225818--upload_2017-4-6_19-15-10.jpg

No problem!

Good luck with the rest of your project :slight_smile:

1 Like