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

Please help with my jump script

Discussion in 'Scripting' started by We4Kingz1, Sep 2, 2015.

  1. We4Kingz1

    We4Kingz1

    Joined:
    Sep 2, 2015
    Posts:
    1
    I want the jump in my game to be proportional to the size of the player




    using UnityEngine;
    using System.Collections;

    public class Jump : MonoBehaviour {

    public float jumpSpeed;
    public bool isGround;

    private Renderer rend;
    private Rigidbody rb;

    void Start(){
    rend = GetComponent<Renderer> ().bounds.size;
    rb = GetComponent<Rigidbody>();
    }

    void jump(){
    rb.AddForce(Vector3.up * jumpSpeed + rend);
    }

    void OnCollisionStay (Collision collisionInfo)
    {
    if(collisionInfo.gameObject.CompareTag("Ground")){
    isGround = true;
    }
    }

    void OnCollisionExit (Collision collisionInfo)
    {

    isGround = false;

    }

    void Update(){

    if(Input.GetButton("Jump") && isGround == true){
    jump();

    }


    }
    }



    I am getting errors like :
    Assets/_scripts/Jump.cs(18,20): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector3'

    Assets/_scripts/Jump.cs(18,20): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3)' has some invalid arguments

    Assets/_scripts/Jump.cs(18,37): error CS0019: Operator `+' cannot be applied to operands of type `UnityEngine.Vector3' and `UnityEngine.Renderer'

    Assets/_scripts/Jump.cs(13,17): error CS0029: Cannot implicitly convert type `UnityEngine.Vector3' to `UnityEngine.Renderer'
     
  2. Chromega1231

    Chromega1231

    Joined:
    Jan 25, 2015
    Posts:
    9
    GetComponent<Renderer>().bounds.size() returns a Vector3. You're trying to assign it to "rend," which you've said is type Renderer. However, if you make "rend" a Vector3 (and you might want to rename it, for clarity), that should fix most of your compilation problems.

    Couple logical error, too, if I might chime in:
    -When you jump, you are calling AddForce. This adds force just for one frame...and that doesn't do much. What you probably want is to add an impulse, which is done most easily by setting the velocity, something like:
    rb.velocity = Vector3.up*jumpSpeed;
    or
    rb.velocity += Vector3.up*jumpSpeed;

    Depending on whether you want to consider your initial velocity (if you want to, say, do a running jump, consider the second.

    I'm not sure what you're trying to do adding the render's bounds to your jump velocity.

    Additionally, I'd usually use GetButtonDown instead of GetButton. GetButtonDown will return true just on the frame you press it, whereas GetButton will return true as long as the button is held down. That means, if, for some reason, isGround isn't immediately cleared, jump will be called twice. And, if you keep jump held down, you'll jump again when you land. Maybe these are OK, but I wanted to clarify.