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

Mathf.Clamp Not Appearing

Discussion in 'Scripting' started by Scorael, Apr 29, 2015.

  1. Scorael

    Scorael

    Joined:
    Apr 12, 2015
    Posts:
    12
    Hi,

    I am going through the tutorials for the space shooter and I am having trouble with the one moving the Spaceship.

    The issue I encounter is that Mathf.Clamp refuses to be typed in. I am using Unity 5 and I am assuming like rigidbody I need to retrieve it somehow now that Unity 5 does not have a shortcut properties.

    Could anyone explain how to do this? I am very new with C#
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    We can't do anything without code. Please post your relevant code bit (in the correct format).
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    mathf isn't like a rigidbody, it's just a class. You use it the same way in unity5 as you did before.

    A rigidbody is a component on a gameobject, they took out the shortcuts which did the
    Code (csharp):
    1.  Rigidbody rigidbody = GetComponent<Rigidbody>();
    for you...
     
    Fajlworks likes this.
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    As with what he said. If you want to use Mathf, you just type Mathf. It's a static class which means it works no matter where you use it.
     
  5. Scorael

    Scorael

    Joined:
    Apr 12, 2015
    Posts:
    12
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    public float speed;

    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody> ();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal,0.0f, moveVertical);
    rb.velocity = movement*speed


    rb.position = new Vector3
    {
    Mathf
    }
    }
    }

    So that's my code at the moment. To clarify, Unity auto-completes Mathf but when I try to type in the Clamp component the auto-complete does not display it
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    its
    Code (csharp):
    1.  new Vector3()
    not
    Code (csharp):
    1. new Vector3 {}
    autocomplete really only works when you're using correct syntax so it can work out what you are likely to be doing...
     
  7. Scorael

    Scorael

    Joined:
    Apr 12, 2015
    Posts:
    12
    Thanks. I didn't notice that one.
    It still isn't working though. I did notice that the auto-complete works when I tried using Mathf.Clamp inside void Start() but won't work anywhere within void FixedUpdate().

    Edit: lol I just noticed I forgot a semi-colon at movement * speed. It's working now. Thanks for the help guys. Really appreciate it :D