- Home /
Question by
Paramotion · Jun 04, 2017 at 03:44 PM ·
transformfloatmonobehaviourcontextexists
transform does not exist in the current context
Just keeping on convert from js to C# the code from Unity, in this script, I'm stuck with the "transform" name.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class FreeMovementMotor : MovementMotor {
Rigidbody charRigidbody;
public float walkingSpeed = 5.0f;
public float walkingSnappyness = 50;
public float turningSmoothing = 0.3f;
public void Start ()
{
// charRigidbody = GetComponent<Rigidbody>();
}
public void FixedUpdate ()
{
//Handle character's movement
Vector3 targetVelocity = movementDirection * walkingSpeed;
Vector3 deltaVelocity = targetVelocity - charRigidbody.velocity;
if(charRigidbody.useGravity)
{
deltaVelocity.y = 0;
}
charRigidbody.AddForce (deltaVelocity * walkingSnappyness, ForceMode.Acceleration);
//Setup player to face facingDirection, or if that's zero, then the movementDirection
Vector3 faceDir = facingDirection;
if(faceDir == Vector3.zero)
{
faceDir = facingDirection;
}
//Make the character rotate towards the target rotation
if(faceDir == Vector3.zero)
{
charRigidbody.angularVelocity = Vector3.zero;
}
else
{
float rotationAngle = AngleAroundAxis (transform.forward, faceDir, Vector3.up);
charRigidbody.angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
}
}
//The angle between dirA and dirB aorund axis
public static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis) //CANT BE A VOID!! float??
{
//Project A and B onto the plane orthogonal target axis
dirA = dirA - Vector3.Project (dirA, axis);
dirB = dirB - Vector3.Project (dirB, axis);
//Find positive angle between A and B
float angle = Vector3.Angle (dirA, dirB);
//Return angle multiplied with 1 or -1
return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);
}
}
in line 45
float rotationAngle = AngleAroundAxis (transform.forward, faceDir, Vector3.up);
it says "the name "transform" does not exist in the current context [Assembly-CSharp]" Is any way to solve this? Or any advice of why I'm getting this?
Comment
Best Answer
Answer by StewVanB · Jun 04, 2017 at 03:54 PM
Are your classes or base classes extending Monobehaviour?
No it wasn't extending $$anonymous$$onoBehaviour
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class $$anonymous$$ovement$$anonymous$$otor
{
/*
This class can be used like an interface.
Inherit from it to define your own movement motor that can control
the movement of characters, enemies, or other entities.
*/
// The direction the character wants to move in, in world space.
// The vector should have a length between 0 and 1.
public Vector3 movementDirection;
// Simpler motors might want to drive movement based on a target purely
public Vector3 movementTarget;
// The direction the character wants to face towards, in world space.
public Vector3 facingDirection;
}
in .JS it was like this:
#pragma strict
/*
This class can be used like an interface.
Inherit from it to define your own movement motor that can control
the movement of characters, enemies, or other entities.
*/
// The direction the character wants to move in, in world space.
// The vector should have a length between 0 and 1.
@HideInInspector
public var movementDirection : Vector3;
// Simpler motors might want to drive movement based on a target purely
@HideInInspector
public var movementTarget : Vector3;
// The direction the character wants to face towards, in world space.
@HideInInspector
public var facingDirection : Vector3;
So I didn't add "$$anonymous$$ovement$$anonymous$$otor : $$anonymous$$onoBehaviour". I just add it after reading your answer, and it seems to work, thank you!
Answer by telgo · Feb 06, 2020 at 04:22 PM
Are you still working on the Angry Bots project? We may be able to help each other...
Your answer
![](https://koobas.hobune.stream/wayback/20220612123500im_/https://answers.unity.com/themes/thub/images/avi.jpg)