Transform does not exist in current context
My code is meant to allow a character to move in all directions rotate and jump when it is touching the ground but why does it say transform does not exit in current context. When i used this code before it work just fine but why not now
using UnityEngine; using System.Collections; public class CharacterMove : MonoBehaviour { public float MoveSpeed; public float JumpHeight; public float RotationSpeed; public bool IsGrounded; public float HitDistance; public LayerMask Layer; public void UpdateStats(){ if (IsGrounded) HitDistance = 1f; else HitDistance = 0.5f; if (Physics.Raycast (transfrom.position-new Vector3 (0.1f,0.1f,0.1f), -transform.up, HitDistance, Layer)) IsGrounded = true; else IsGrounded = false; } public void FixedUpdate(){ float XMOVE = Input.GetAxis ("Horizontal"); float YMOVE = Input.GetAxis ("Vertical"); float Turn = Input.GetAxis ("Mouse X"); transform.Translate (new Vector3 (-YMOVE, 0, XMOVE) * MoveSpeed * Time.deltaTime); transform.Rotate (new Vector3 (0, Turn, 0) * RotationSpeed * Time.deltaTime); if (Input.GetKeyDown (KeyCode.Space) && (IsGrounded == true)) { GetComponent<Rigidbody> ().AddForce (new Vector3 (-YMOVE, JumpHeight, XMOVE) * (MoveSpeed), ForceMode.Impulse); } } }
Answer by Xeonity_ · Sep 04, 2017 at 05:42 PM
using System.Collections;
using UnityEngine;
using System;
public class CharacterMove : MonoBehaviour {
public float MoveSpeed;
public float JumpHeight;
public float RotationSpeed;
public bool IsGrounded;
public float HitDistance;
public LayerMask Layer;
public void UpdateStats(){
if (IsGrounded)
HitDistance = 1f;
else
HitDistance = 0.5f;
if (Physics.Raycast (transfrom.position-new Vector3 (0.1f,0.1f,0.1f), -transform.up, HitDistance, Layer))
IsGrounded = true;
else
IsGrounded = false;
}
public void FixedUpdate(){
float XMOVE = Input.GetAxis ("Horizontal");
float YMOVE = Input.GetAxis ("Vertical");
float Turn = Input.GetAxis ("Mouse X");
transform.Translate (new Vector3 (-YMOVE, 0, XMOVE) * MoveSpeed * Time.deltaTime);
transform.Rotate (new Vector3 (0, Turn, 0) * RotationSpeed * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.Space) && (IsGrounded == true)) {
GetComponent<Rigidbody> ().AddForce (new Vector3 (-YMOVE, JumpHeight, XMOVE) * (MoveSpeed), ForceMode.Impulse);
}
}
}