Make the object rise up when clicking on the screen?

I’m making a new mobile game and I have a problem with the code of the object.
The object is placed on (y 0 ) and I want to make it rise up ( not left and right or forward and backwards), just when I hold the mouse click down and when releasing the mouse click the object stops rising up and stays in place?
Can someone help me please?
Thank you!!

Simplest thing would be to check whether the mouse button is currently help in the Update statement, and if so, add a small amount (constant * Time.deltaTime) to the y-position of the object. It’s a pretty simple statement. Why don’t you try writing it and post what you come up with. You’ll probably want to use Input.GetMouseButton (Unity - Scripting API: Input.GetMouseButton) to detect the click.

Thank you for your reply.
I already use the Input.GetMouseButton in the script, but what happens is that the object rises up and never stops, just when I click the mouse button once.
and the object needs to destroy other objects when colliding with them but it just does nothing?
I provided the script below.

4549513–422269–PlayerController.cs (1.22 KB)

Please use code tags for showing code. It’s easier for everyone.

It seems that the issue with your code is that on mouse up, you’re setting “goingUp” to false, but you never use it anywhere. You probably need to update your GoUp() method to do one thing or another depending on the current value of goingUp.

I’m not still a newbie and don’t know exactly how to do that.
Can you please edit my code, because that would be a lot helpful and I could understand it better?

Something like this might work:

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

public class PlayerController : MonoBehaviour
{
    [Header(" Physics ")]
    Rigidbody rig;


    [Header(" Up Stuff ")]
    public float upSpeed;
    bool goingUp;

    // Start is called before the first frame update
    void Start()
    {
        // Store the rigidbody
        rig = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            goingUp = true;
            UpdateVelocity();
        }

        else if (Input.GetMouseButtonUp(0))
        {
            goingUp = false;
            UpdateVelocity();
        }
    }

    private void UpdateVelocity()
    {
        Vector3 vel = rig.velocity;
        vel.y = goingUp ? upSpeed : 0;
        rig.velocity = vel;
    }
   
}

It worked, but just when I check (use gravity) in the Rigidbody. ( when I don’t check the use gravity the ball just rises up and does not stop).
The problem is now that whenever I release the mouse click the object falls down, but I want the object to stay in place.
How can I do that?
Thank you for your help. I appreciate it!

If you want an object to not be affected just by gravity, you can set the rigidbody not to use gravity. If you want it to be unaffected by any physics forces at all (such as other objects colliding with it) you can set it to isKinematic = true. It’s not clear which behavior you want on this object. Should it ever be affected by physics forces? If not, set isKinematic = true, and then use MovePosition to move it upward each frame.

It worked!!
But the object does not destroy the other objects when colliding with them.
How can I solve?

 private void OncollistionStay(Collision other)
    {

        if (!goingUp)

        {

            Destroy(other.transform.parent.gameObject);


        }


    }

Thank you for your help. I appreciate it!

You misspelled “OncollistionStay”. It’s “OnCollisionStay”. You’ve got one lower case letter wrong there, and you’ve got an extra “t”. Make sure the method names are exact.

Also, do you only want it to destroy the other objects when it’s not moving? Right now you’re only destroying when goingUp is false. That seems backwards. Also, you’re destroying the parent of the other object. Although that should destroy the other object as well, maybe you just want to destroy other.gameObject? If that’s still not working, you should check whether the collision is happening at all.

I tried this code:

private void OnCollisionEnter(Collision other)
    {

        if (!goingUp) ;
   
        {
         

             Destroy(other.transform.parent.gameObject);
          

        }


    }

and what happens is that the object when it collides with the other objects, the other object gets destroyed but the object then stops even if I hold the mouse button down. ( I need to click every time to destroy an object)
you need to know that I have a lot of objects in a raw and the moving object has not to stop, it needs to go throw the objects and not stop, just when I release the mouse button.

You’ve got a stray semicolon on line 4, by the way. I assume that’s a copy/paste issue, because that code won’t compile the way you have it.

As for the object stopping, it would only do that if it’s not set to isKinematic. If this is a moving platform which should not be affected by other forces acting on it, you should set the rigidbody to isKinematic, otherwise other collisions will slow/stop/reverse the movement of the object.

I checked the (isKinematic) and removed the code in line 4.
The player then does not respond to the clicks, it does not move at all.
And I noticed that when the player is rising up, it kinda lag a bit as it wants to drag itself down.

Sorry if this has been disjointed. Taking this back up to a higher level, you’re going to need to pick which physics path you want to take for your player. You’ve got two general options:

  • Kinematic: The player can exert physics forces on other objects, but does not receive physics forces. It’s unaffected by gravity, and if you want it to move, you’ll need to move it manually with methods like MovePosition and MoveRotation. The rigidbody velocity will no longer affect the object, and instead of setting the velocity to go up, you’ll want to use MovePosition within FixedUpdate to move the player upward a little bit each FixedUpdate.
  • Non-Kinematic: The player will be subject to the effects of physics. You can either move it with AddForce, or by setting its velocity manually. But if someone bumps into the player, it will slow/stop the player.

There may be some subtle in-betweens to these approaches, but these are your general choices. Right now, you’ve got a bit of both, which is going to be a little messy.

So what do you suggest to do?

It depends on what kind of behavior you want. But a single question should hopefully point you in the right direction: Are there any times where your player should behave like a normal rigidbody, such that other objects in the scene can push it around and exert force on it? If your player doesn’t need to behave like a normal rigidbody, then you should probably set it to kinematic, and move it via MovePosition, rather than adjusting its velocity.

Maybe you can explain the overall functionality you want to get from your player. What other motion will the player have, other than simply moving up and standing still?

So the player destroys the other objects when colliding with them and there are enemies too, so when the player collides with them you lose the game.
Do this behavior works with using kinematic?
and yes the player just rises up and stops when the mouse button is not clicked.

Sounds fine to use isKinematic, then. It’s also possible you don’t need a rigidbody on the player at all, but that might lead to some cases where collisions between the player and other objects aren’t detected. So, I guess you should keep the rigidbody for now and set it to Is Kinematic. Them, you FixedUpdate would use MovePosition instead of setting the velocity. Something like this:

void FixedUpdate() {
    if (goingUp) {
        rig.MovePosition(transform.position + transform.up * upSpeed * Time.deltaTime);
    }
}

I inserted this code:

    void FixedUpdate()
    {
        if (Input.GetMouseButtonDown(0))
        {
            rig.MovePosition(transform.position + transform.up * upSpeed * Time.deltaTime);

        }


    }

what happens is the player moves just a bit a stops even if I hold the mouse button down.
and the player does not collide with the other objects, he goes throw them.
to give you a good example of how I want the player to behave in the game,
it’s like the mobile game “Stack Ball”, but in my game, the ball is beginning from 0 and rises up.

Change GetMouseButtonDown to GetMouseButton. They’re different. The first only fires once, on the exact frame where you first press the button. The second is true the entire time the button is held.

As for the collision issue, you’ll need to understand that Unity has some fairly complex rules about whether any two objects with colliders will actually trigger collision events. If you look at the “Collision action matrix” on this page, you’ll see that it depends strongly on the isKinematic state, whether an object is static, etc:

In your case, it sounds like changing the player to Kinematic means you’ll only collider with other objects that have non-kinematic rigidbodies. You’ll need to play around with things to get it working the way you want based on the kinds of collision events Unity will raise. Note that Trigger events fire more liberally than Collision events, so it might be necessary to convert some of your colliders into triggers if you can’t get them to collide as expected using OnCollisionEnter.