Forum rules - please read before posting.

How do I push and pull an object of any type cabinet using AC?

Pulling and pushing 3d objects.

Comments

  • edited June 2021

    It really does depend on what exactly it is you're after - this is a broad topic.

    Are you referring to having an object move automatically using Hotspot interactions? You can use the Object: Transform Action for that.

    To have a more dynamic system, i.e. have the Player character physically "drag" an object around as they move, you'd likely need a custom script or third-party asset as the exact behaviour will be specific to your project.

    Can you share more details?

  • edited June 2021

    To have a more dynamic system, i.e. have the Player character physically "drag" an object around as they move, you'd likely need a custom script or third-party asset as the exact behaviour will be specific to your project.

    Can you share more details?


    **That's right, physically a player pushing or pulling an object (statue, counter) in classic resident evil style.

    Also considering the push and pull animations.**

  • Again, I must give the disclaimer that AC is not intended for Resident Evil-style games, but traditional adventure games. The Combat Example package you're using is provided to demonstrate how AC can be extended for such mechanics with custom scripting. You will need to be familar with coding if this is the kind of game you wish to make with AC.

    You'll have an easier time making this kind of mechanic if you introduce some limitations, such as pushing only, and only moving in pre-set directions (north, east, south, west) by a fixed distance.

    Here's an example script that you can attach to a pushable object that has a Collider and Rigidbody attached:

    using System.Collections;
    using UnityEngine;
    using AC;
    
    [RequireComponent (typeof (Rigidbody))]
    public class Pushable : MonoBehaviour
    {
    
        private Rigidbody _rigidbody;
        public float distance = 1f;
        public float duration = 1f;
    
    
        private void Awake ()
        {
            _rigidbody = GetComponent<Rigidbody> ();
        }
    
    
        public void Push ()
        {
            Vector3 playerPosition = KickStarter.player.transform.position;
            Vector3 playerDirection = (_rigidbody.position - playerPosition).normalized;
    
            Vector3 bestDirection;
            float maxDot = Vector3.Dot (Vector3.forward, playerDirection);
            if (maxDot > 0f)
            {
                bestDirection = Vector3.forward;
            }
            else
            {
                maxDot *= -1f;
                bestDirection = -Vector3.forward;
            }
    
            float rightDot = Vector3.Dot (Vector3.right, playerDirection);
            if (rightDot > maxDot)
            {
                bestDirection = Vector3.right;
            }
            else if (-rightDot > maxDot)
            {
                bestDirection = -Vector3.right;
            }
    
            Push (bestDirection);
        }
    
    
        private void Push (Vector3 direction)
        {
            if (_rigidbody.SweepTest (direction, out RaycastHit hitInfo, distance))
            {
                // Cannot move that way
                return;
            }
    
            StopAllCoroutines ();
            Vector3 targetPosition = _rigidbody.position + (direction * distance);
            StartCoroutine (DoMove (targetPosition));
        }
    
    
        private IEnumerator DoMove (Vector3 targetPosition)
        {
            if (KickStarter.stateHandler) KickStarter.stateHandler.EnforceCutsceneMode = true;
            Vector3 startPosition = _rigidbody.position;
    
            float time = 0f;
            while (time < duration)
            {
                time += Time.deltaTime;
                _rigidbody.position = Vector3.Lerp (startPosition, targetPosition, time / duration);
                yield return null;
            }
            _rigidbody.position = targetPosition;
    
            if (KickStarter.stateHandler) KickStarter.stateHandler.EnforceCutsceneMode = false;
        }
    
    }
    

    This script can be used to move the object away from the Player in the nearest cardinal direction by a set distance - provided that it won't collide with anything along the way.

    To have it move as part of a Hotspot interaction, use the Object: Call event or Object: Send message Action to call the component's Push function.

    This won't affect the Player's motion/animation, but that can be added later once the motion of the pushable object is working.

  • edited June 2021

    It didn't work. Is there any button in the escript that triggers when you press?

  • Try this instead, which will update the Console with progress information:

    using System.Collections;
    using UnityEngine;
    using AC;
    
    [RequireComponent (typeof (Rigidbody))]
    public class Pushable : MonoBehaviour
    {
    
        private Rigidbody _rigidbody;
        public float distance = 1f;
        public float duration = 1f;
    
    
        private void Awake ()
        {
            _rigidbody = GetComponent<Rigidbody> ();
        }
    
    
        public void Push ()
        {
            Vector3 playerPosition = KickStarter.player.transform.position;
            Vector3 playerDirection = (_rigidbody.position - playerPosition).normalized;
    
            Vector3 bestDirection;
            float maxDot = Vector3.Dot (Vector3.forward, playerDirection);
            if (maxDot > 0f)
            {
                bestDirection = Vector3.forward;
            }
            else
            {
                maxDot *= -1f;
                bestDirection = -Vector3.forward;
            }
    
            float rightDot = Vector3.Dot (Vector3.right, playerDirection);
            if (rightDot > maxDot)
            {
                bestDirection = Vector3.right;
            }
            else if (-rightDot > maxDot)
            {
                bestDirection = -Vector3.right;
            }
    
            DoPush (bestDirection);
        }
    
    
        private void DoPush (Vector3 direction)
        {
            Debug.Log ("Push requested in direction: " + direction);
            if (_rigidbody.SweepTest (direction, out RaycastHit hitInfo, distance))
            {
                // Cannot move that way
                return;
            }
    
            StopAllCoroutines ();
            Vector3 targetPosition = _rigidbody.position + (direction * distance);
            StartCoroutine (DoMove (targetPosition));
            Debug.Log ("Pushing..");
        }
    
    
    
        private IEnumerator DoMove (Vector3 targetPosition)
        {
            if (KickStarter.stateHandler) KickStarter.stateHandler.EnforceCutsceneMode = true;
            Vector3 startPosition = _rigidbody.position;
    
            float time = 0f;
            while (time < duration)
            {
                time += Time.deltaTime;
                _rigidbody.position = Vector3.Lerp (startPosition, targetPosition, time / duration);
                yield return null;
            }
            _rigidbody.position = targetPosition;
    
            if (KickStarter.stateHandler) KickStarter.stateHandler.EnforceCutsceneMode = false;
        }
    
    }
    

    If nothing shows, it's Push function isn't being called. This isn't automatic - you need to manually call this function using one of the Actions I mentioned above. You'll need to share screenshots showing your setup for me to see what the problem might be.

  • First photo.

    I added the script in the cube object and getting close by pressing the button I use to open doors, nothing happens.

    I've already tried all the buttons on the joystick, I also tried to go against the cube (as if I was pushing the cube) and nothing happens.

    https://uploaddeimagens.com.br/imagens/OCj650M

    Second and third Photo.

    I added a hotspot and an interaction to the cube. I don't know if I did the right thing.

    as you can see no warning or error appears

    Need some tag on the player?

    https://uploaddeimagens.com.br/imagens/D_ddODk

    https://uploaddeimagens.com.br/imagens/2Uv5K1w

  • You need to use either of the Actions to affect the above component - not the Player.

    If you use the Object: Send message Action, uncheck Send to Player?, set Object to affect to the object in your scene with the Pushable component, Message to send to Custom, and Method name to Push.

    If you use the Object: Call event Action, drag in the object in your scene with your Pushable component, and then select Pushable -> Push from the dropdown field beside it.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.