Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Charged Shot Script Help - Payment In Assets ?

Discussion in 'Scripting' started by Deleted User, Nov 11, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hello,

    Anybody willing to help me with my movement script ? I'm offering payment in potential assets you might like from my collection : -

    https://www.assetstore.unity3d.com/en/#!publisher/6359

    I'm wanting to apply force to a gameObject, let's say a 3D Cube in this instance. I need to be able to click on the object, select how much power to apply based on some sort of visual feedback, dots, ring, colour bar, whatever, and then release and the gameObject is then pushed forward at the chosen force ? For a better visual example of what I want, please see : -

    http://www.flashgames247.com/play/3280.html

    Obviously that is a 2d game, I'm wanting movement like that, but in 3d. If I was to expand it any further at this stage, I would add gameObject rotation to 'face' the direction it is aiming at.

    I'm designing a very simple game, and need to get the basics of what I'm trying to achieve up and running, if you can help I'm willing to share a few assets from my store collection in payment via voucher(s) that can be redeemed via the asset store.

    Can you help ?

    reply here, or pm for a private conversation.

    Thanks
     
  2. Deleted User

    Deleted User

    Guest

    Ok, here is where my limited coding experience has landed me :-

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PushMe3 : MonoBehaviour {
    4.  
    5.     public float Power = 250f;
    6.     public float maxPower = 1000f;
    7.  
    8.     void Update () {
    9.  
    10.         if (Input.GetMouseButtonDown(0)) {
    11.  
    12.             Power += Time.deltaTime;
    13.  
    14.         }
    15.         if (Input.GetMouseButtonUp(0) && (Power <= maxPower)) {
    16.  
    17.             GetComponent<Rigidbody>().AddForce(-transform.forward * Power);
    18.             GetComponent<Rigidbody>().useGravity = true;
    19.  
    20.             // Reset Power
    21.  
    22.             Power = 250f;
    23.  
    24.         }
    25.     }
    26. }
    Problems : -

    Power DOES NOT increase when I hold the mouse button down, only a fraction is added ?

    Next problem, my checking function for when the mouse button is relased, no idea if this works as intended, for instance, if I set my Power to 999.99, and it then exceeds the maxPower of 1000 then nothing happens at all, so obviously my code to check the power amount is crap, can anyone please, please help elaborate for me ?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    To make power increase every frame you want to check Input.GetMouseButton.

    I'm not sure what the if check is supposed to do. If I'm reading it right I would code it something like this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PushMe3 : MonoBehaviour {
    4.  
    5.     public float Power = 250f;
    6.     public float maxPower = 1000f;
    7.  
    8.     void Update () {
    9.  
    10.         if (Input.GetMouseButton(0)) {
    11.  
    12.             Power += Time.deltaTime;
    13.             Power = Mathf.Clamp(Power, 0, maxPower);
    14.  
    15.         }
    16.         if (Input.GetMouseButtonUp(0)) {
    17.  
    18.             GetComponent<Rigidbody>().AddForce(-transform.forward * Power);
    19.             GetComponent<Rigidbody>().useGravity = true;
    20.  
    21.             // Reset Power
    22.  
    23.             Power = 250f;
    24.  
    25.         }
    26.     }
    27. }
     
    Last edited: Nov 12, 2015
  4. Deleted User

    Deleted User

    Guest

    Hi,

    Thanks for your input,

    Still doesn't work though, no force is increased ( other than 1 increment ).

    this is what I want

    on my player, I will attach this script
    in game, then I want : -

    click mouse over player, force increases over time from default value and no more than maximum value, on mouse release, addforce forward at the force amount stored before releasing, minimum amount of force is applied if just clicked, and if user holds for too long, then maximum amount of force is applied only, I don't know why I'm unable to explain this more simply ?

    Please see this game example for clarity : -

    http://www.flashgames247.com/play/3280.html

    user clicks on turtle, is able to pull back anywhere between a minimum and maximum amount of force that can be applied to propel it forward.
     
    Last edited by a moderator: Nov 12, 2015