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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can you set a float equal to an integer that is multiplied?

Discussion in 'Scripting' started by PAPERWRLD, Oct 4, 2016.

  1. PAPERWRLD

    PAPERWRLD

    Joined:
    Jul 12, 2016
    Posts:
    12
    For my game I am creating a sledding race where you and some friends mash to get ahead. I'm trying to make it so the character's float of speed multiplies by the number of times the player taps the button. In the script I'm also trying to have the speed slowly diminish so you can't mash a bunch and ride it out at high speeds. I'm fairly new to coding so pardon my mistakes.

    using UnityEngine;
    using System.Collections;

    public class Movement : MonoBehaviour
    {
    public float slideSpeed;
    public int moveSpeed;
    public int turnSpeed;
    float timeLeft = 1f;

    void Start()
    {
    Debug.Log(moveSpeed * 0.1);
    Debug.Log(turnSpeed * 0.1);
    slideSpeed = Debug.Log(moveSpeed * 0.1);

    }

    void Update()
    {

    if (Input.GetKeyUp(KeyCode.A) || (Input.GetKeyUp(KeyCode.D)))
    {
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    moveSpeed += 1;

    }
    ;

    if (Input.GetKeyUp(KeyCode.A))
    {
    transform.Translate(Vector3.left * turnSpeed * Time.deltaTime);
    turnSpeed += 1;
    }


    if (Input.GetKeyUp(KeyCode.D))
    {
    transform.Translate(Vector3.right * turnSpeed * Time.deltaTime);
    turnSpeed += 1;
    }

    if (moveSpeed > 0)
    {
    timeLeft -= Time.deltaTime;
    moveSpeed -= 1;
    }
    if (turnSpeed > 0)
    {
    timeLeft -= Time.deltaTime;
    turnSpeed -= 1;

    }
    }
    }
     
    Last edited: Oct 4, 2016
  2. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Sounds fun!

    But... what is the question?
    For the topic one - yes, you can, since float can hold any int (widening assignment), but somehow I doubt that's all you wanted to ask.
     
  3. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
  4. PAPERWRLD

    PAPERWRLD

    Joined:
    Jul 12, 2016
    Posts:
    12
    Sorry man I wasn't sure how. I'll change it when I get home.
     
    Last edited: Oct 4, 2016
  5. PAPERWRLD

    PAPERWRLD

    Joined:
    Jul 12, 2016
    Posts:
    12
    In my game I want to mash buttons to speed up. When I have tried it in my script I can't seem to get it to work. I need some help creating the correct formula. I was hoping to have the buttons add to an integer which then adds to the speed. However I don't want jumpy movements. Thanks and sorry if this is still confusing.
     
  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Hej, Swedish Fish. (my Swedish lessons are really paying off! :p)

    To answer your question, yes, you can assign an integer to a float since there is an implicit type cast. But...

    This in particular isn't valid syntax. I'm assuming you added the Debug call for logging, but you're attempting to assign the result of Debug.Log to slideSpeed, but that function doesn't return anything.

    Next, when working with floats you'll generally need to add the float type to constants, so 0.1f instead of 0.1. Without that the compiler assumes a double, which can't directly be assigned to a float since there can be loss of precision.

    And finally, moveSpeed * 0.1f may be problematic since moveSpeed is an int. I never remember these types of compiler rules off the top of my head, but potentially an int multiplied by something less than 1 will give you 0. That is assuming the compiler interprets all of that as an integer. But it may implicitly cast all of that to a float for you.

    All that being said, why not just make your various speeds into floats? It will eliminate all of the funky type casting and such.
     
  7. PAPERWRLD

    PAPERWRLD

    Joined:
    Jul 12, 2016
    Posts:
    12
    Haha mine too. Thanks for the response. Would it be possible to add 0.1f to my speed every time I pressed the button. Then have it subtract 0.5f every second or so?
     
  8. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Yes, if those values are floats.
     
  9. PAPERWRLD

    PAPERWRLD

    Joined:
    Jul 12, 2016
    Posts:
    12
    Well that was easy. Thanks everyone