(Unity2D) Having trouble tossing a rect across the screen using an analog stick on a gamepad.

Started by
0 comments, last by Hashbrown 5 years, 10 months ago

Hello all :) 

I want to use the right analog stick of my gamepad for throwing a rect based on the angle and intensity I flicked the analog stick. So if I flick the analog stick a little to the right, the less force is applied to the rect's rigidbody velocity property.  I should be able to move the character left and right and also toss this rect at the same time.  A good example of this gameplay mechanic would be Skate (xbox360). Granted, Skate is a lot more complex and 3D, and I just want to toss a rect. 

So far, I've kind of figured it out, but incredibly dissatisfied with the results.

rectforum.gif

I'm able to to get the direction of my flick, but it's so sensitive, sometimes I repeatedly toss the rect completely upwards. I also don't feel so much control over strength I toss the rect. Long story short, I don't feel as much control over my flick functionality :(

The functionality is only one script, If any of you have suggestions on improving this functionality it would be greatly appreciated. I'll share the code below, you can also download this little project. Its made for playing with a gamepad though. thanks in advance!


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

public class Player : MonoBehaviour {

    [SerializeField]
    private GameObject boxObject;

    private Rigidbody2D rigidbody;

    private Vector2 leftInput;
    private Vector2 rightInput;
    private float timeFlicking = 0.0f;

    // Flags
    private bool flicking = false;

    private void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {

        // Will use later
        if (flicking) timeFlicking += Time.deltaTime;


        leftInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        rightInput = new Vector2(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"));
        float rightInputMagnitude = rightInput.magnitude;


        // No Analog Stick movement and make sure we're not flicking already
        if (rightInput != Vector2.zero && !flicking) {
            Debug.Log("Flicking!");
            CreateBox();
            flicking = true;
        }

        if (rightInput == Vector2.zero) {
            if (flicking) {

                flicking = false;
            }
        }

    
        Vector2 newVelocity = leftInput * new Vector2(10, 10);
        newVelocity.y = 0;
        rigidbody.velocity = newVelocity; 
    }

    private void CreateBox () {

        GameObject box = Instantiate(boxObject, transform.position, Quaternion.identity);
        Rigidbody2D rb = box.GetComponent<Rigidbody2D>();
        Vector2 dir = rightInput.normalized;
        box.transform.position = (Vector2) box.transform.position + dir;
      	
        rb.velocity = rightInput * new Vector2(40, 40);
    }
}

 

Project (6mbs):
https://files.fm/u/ru8p9rgs

This topic is closed to new replies.

Advertisement