using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysics))]
public class Playercontroller : MonoBehaviour {
// Player Handling
public float speed = 10;
public float acceleration = 7;
private float currentSpeed;
private float targetSpeed;
private Vector2 amounttomove;
private PlayerPhysics PlayerPhysics;
void Start () {
PlayerPhysics = GetComponent<PlayerPhysics>();
}
// Update is called once per frame
void Update () {
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = incrementTowards (currentSpeed, targetSpeed, acceleration);
}
// increase n toward target by speed
private float incrementTowards(float n, float target, float a){
if (n == target) {
return n;
}
else {
float dir = Mathf.sign(target - n) // must n be increased or decreased to get closer to target
n += a * Time.deltatime * dir;
return (dir == Mathf.Sign(target-n))? n: // if n has passed target then return target, otherwise return n
}
}
}
Hi all, i`m new to this script thingy and i was wondering what i did wrong. Any help?
When posting questions like this one, please give a description of the problem and include a copy of the error in the Console window. There are a number of issues here:
- You are missing a ‘;’ at the end of line 33.
- Case matters. ‘deltatime’ should be ‘deltaTime’ on line 34, and ‘sign’ should be ‘Sign’ on line 35.
- On line 35, you have not correctly implemented what you indicate in your comment.
- Variable names and class names should never be the same. By convention, variable names should start with a lower case letter. So ‘PlayerPhysics’ the variable should be ‘playerPhysics’ on lines 15 and 18.
- While not causing a compile problem, by convention, function names should start with a upper case letter. So ‘increment’ should be ‘Increment’.
Here is a copy of your script that should compile:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysics))]
public class Playercontroller : MonoBehaviour {
// Player Handling
public float speed = 10;
public float acceleration = 7;
private float currentSpeed;
private float targetSpeed;
private Vector2 amounttomove;
private PlayerPhysics playerPhysics;
void Start () {
playerPhysics = GetComponent<PlayerPhysics>();
}
// Update is called once per frame
void Update () {
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementTowards (currentSpeed, targetSpeed, acceleration);
}
// increase n toward target by speed
private float IncrementTowards(float n, float target, float a){
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n)) ? n : target; // if n has passed target then return target, otherwise return n
}
}
}